Scala Option[(A, B)] 模式匹配

发布于 2024-11-07 18:44:21 字数 2022 浏览 6 评论 0原文

我正在编写一个 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))): 错误:的参数数量错误:(x: (java.lang.String, Option[java.lang.String]))Some[ (java.lang.String, Option[java.lang.String])]

我不确定出了什么问题。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味超标? 2024-11-14 18:44:21

Some 不会提取为两个值,而是提取为一个值。如果你想匹配某个对,那么你需要在括号上加倍:

case Some( (jType, Some(iType)) ) => (jType, iType)

如果你可以使用箭头约定作为提取器,那就太好了,但遗憾的是这似乎不起作用:

case Some(jType -> Some(iType)) => (jType, iType)

更新

,考虑到您正在使用一个选项,您可以利用它的单子性质并简单地映射该事物:

val tpes = SqlTypesToJavaTypeNames.get(dataType)
val (javaType, importType) =
  tpes map { case (a,b) => (a, b.orNull) } getOrElse { throw ... }

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:

case Some( (jType, Some(iType)) ) => (jType, iType)

It would be nice if you could use the arrow convention as an extractor, but that sadly doesn't seem to work:

case Some(jType -> Some(iType)) => (jType, iType)

UPDATE

Alternatively, given that you're using an Option, you could take advantage of its monadic nature and simply map over the thing:

val tpes = SqlTypesToJavaTypeNames.get(dataType)
val (javaType, importType) =
  tpes map { case (a,b) => (a, b.orNull) } getOrElse { throw ... }
把时间冻结 2024-11-14 18:44:21

您缺少内部括号(因为您有一个 Option[(A, B)]

case Some( (jType, Some(iType)) ) => 
case Some( (jType, _) )           =>
case None                         =>

从您的方法的外观来看,您似乎可以进一步简化:

SqlTypesToJavaTypeNames.get(dataType) map { case (jType, maybeIType) => jType -> maybeIType.orNull } getOrElse error("Unmapped : " + dataType)

You are missing the inner parens (because you have an Option[(A, B)]:

case Some( (jType, Some(iType)) ) => 
case Some( (jType, _) )           =>
case None                         =>

From the looks of your method, it seems like you could simplify even more:

SqlTypesToJavaTypeNames.get(dataType) map { case (jType, maybeIType) => jType -> maybeIType.orNull } getOrElse error("Unmapped : " + dataType)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文