Scala 模式匹配中的自动拆箱
在下面的代码中,我收到一个编译错误,指出“x”上存在类型不匹配:
val someRef: java.lang.Long = 42L
someRef match {
case x: Long => println("The answer: " + x)
case _ => println("Unknown")
}
How do I get Scala to auto-unbox someRef in the match statements?
In the following code, I am getting a compilation error stating that I have a type mismatch on 'x':
val someRef: java.lang.Long = 42L
someRef match {
case x: Long => println("The answer: " + x)
case _ => println("Unknown")
}
How do I get Scala to auto-unbox someRef in the match statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类型系统不知道这个级别的拳击。但它确实知道,如果存在
Any
,则装箱的Long
实际上(大概)应该只是一个Long
(来自 < code>AnyVal 类继承树的一部分)。所以:The type system doesn't know about boxing at this level. But it does know that if there is an
Any
, a boxedLong
is really (presumably) supposed to be just aLong
(from theAnyVal
part of the class inheritance tree). So: