Scala Option[(A, B)] 模式匹配
我正在编写一个 Java 代码生成器。
我有一个不可变的 Map,其中包含从 java.sql.Types
[Int] 到 (String, String)
元组的映射,其中第一个值是 Java 类型,第二个是 Java 包,如果默认情况下未导入类型(java.lang
),则从中导入类型:
val SqlTypesToJavaTypeNames =
Map(Types.BIGINT -> ("Long", None),
Types.BINARY -> ("byte[]", None),
Types.BIT -> ("Boolean", None),
Types.BOOLEAN -> ("Boolean", None),
Types.CHAR -> ("String", None),
Types.DATE -> ("Date", Some("java.sql.Date")),
Types.DECIMAL -> ("BigDecimal", Some("java.math.BigDecimal")),
Types.DOUBLE -> ("Double", None),
Types.FLOAT -> ("Float", None),
Types.INTEGER -> ("Integer", None),
Types.LONGNVARCHAR -> ("String", None),
Types.LONGVARCHAR -> ("String", None),
Types.NCHAR -> ("String", None),
Types.NUMERIC -> ("BigDecimal", None),
Types.NVARCHAR -> ("String", None),
Types.REAL -> ("Float", None),
Types.SMALLINT -> ("Short", None),
Types.SQLXML -> ("String", None),
Types.TIME -> ("Time", Some("java.sql.Time")),
Types.TIMESTAMP -> ("Timestamp", Some("java.sql.Timestamp")),
Types.TINYINT -> ("Byte", None),
Types.VARCHAR -> ("String", None))
我正在尝试对此地图的搜索进行模式匹配,其中 dataType< /code> 是来自数据库元数据的
java.sql.Types
值:
val (javaType, importType) =
SqlTypesToJavaTypeNames.get(dataType) match {
case Some(jType, Some(iType)) => (jType, iType)
case Some(jType, None) => (jType, null)
case None => throw new IllegalStateException("Unknown translation to Java type for SQL type " + dataType)
}
编译器在第一个 case
上给我一个错误(以 case Some( 开头) jType, Some(iType))
): 错误:
我不确定出了什么问题。
I am writing a Java code generator.
I have an immutable Map that contains a mapping from java.sql.Types
[Int] to a tuple of (String, String)
where the first value is a Java type and the second a Java package from which to import the type if it is not imported by default (java.lang
):
val SqlTypesToJavaTypeNames =
Map(Types.BIGINT -> ("Long", None),
Types.BINARY -> ("byte[]", None),
Types.BIT -> ("Boolean", None),
Types.BOOLEAN -> ("Boolean", None),
Types.CHAR -> ("String", None),
Types.DATE -> ("Date", Some("java.sql.Date")),
Types.DECIMAL -> ("BigDecimal", Some("java.math.BigDecimal")),
Types.DOUBLE -> ("Double", None),
Types.FLOAT -> ("Float", None),
Types.INTEGER -> ("Integer", None),
Types.LONGNVARCHAR -> ("String", None),
Types.LONGVARCHAR -> ("String", None),
Types.NCHAR -> ("String", None),
Types.NUMERIC -> ("BigDecimal", None),
Types.NVARCHAR -> ("String", None),
Types.REAL -> ("Float", None),
Types.SMALLINT -> ("Short", None),
Types.SQLXML -> ("String", None),
Types.TIME -> ("Time", Some("java.sql.Time")),
Types.TIMESTAMP -> ("Timestamp", Some("java.sql.Timestamp")),
Types.TINYINT -> ("Byte", None),
Types.VARCHAR -> ("String", None))
I am trying to pattern match on a search of this map, where dataType
is the java.sql.Types
value from a database metadata:
val (javaType, importType) =
SqlTypesToJavaTypeNames.get(dataType) match {
case Some(jType, Some(iType)) => (jType, iType)
case Some(jType, None) => (jType, null)
case None => throw new IllegalStateException("Unknown translation to Java type for SQL type " + dataType)
}
The compiler is giving me an error on the first case
(starts with case Some(jType, Some(iType))
): error: wrong number of arguments for <none>: (x: (java.lang.String, Option[java.lang.String]))Some[(java.lang.String, Option[java.lang.String])]
I'm not sure what is wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Some
不会提取为两个值,而是提取为一个值。如果你想匹配某个对,那么你需要在括号上加倍:如果你可以使用箭头约定作为提取器,那就太好了,但遗憾的是这似乎不起作用:
更新
,考虑到您正在使用一个选项,您可以利用它的单子性质并简单地映射该事物:
Some
doesn't extract to two values, it extracts to one. If you want to match some pair, then you need to double-up on the parentheses:It would be nice if you could use the arrow convention as an extractor, but that sadly doesn't seem to work:
UPDATE
Alternatively, given that you're using an Option, you could take advantage of its monadic nature and simply map over the thing:
您缺少内部括号(因为您有一个
Option[(A, B)]
:从您的方法的外观来看,您似乎可以进一步简化:
You are missing the inner parens (because you have an
Option[(A, B)]
:From the looks of your method, it seems like you could simplify even more: