开发者

java.lang.Boolean to scala.Boolean question

开发者 https://www.devze.com 2023-03-07 13:16 出处:网络
georgii@gleontiev:~$ scala Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
georgii@gleontiev:~$ scala
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val jbool = java.lang.Boolean.TRUE    
jbool: java.lang.Boolean = true

scala> val sbool = true         
sbool: Boolean = true

scala> def sboolMethod(sbool: Boolean) = print("got scala.Boolean " + sbool)              
sboolMethod: (sbool: Boolean)Unit

scala> sboolMethod(sbool)
got scala.Boolean true

scala> sboolMethod(jbool)
<console>:9: error: type mismatch;
 found   : java.lang.Boolean
 required: scala.Boolean
       sboolMethod(jbool)
                   ^

scala> implicit def jbool2sbool(bool: java.lang.Boolean): scala.Boolean = bool.booleanValue
jbool2sbool: (bool: java.lang.Boolean)Boolean

scala> sboolMethod(jbool)                   开发者_JAVA百科                                               
got scala.Boolean true

The question is: why isn't there a default implicit conversion from java.lang.Boolean to scala.Boolean? The question also stands for java.lang.Long vs scala.Long and probably other standard types (haven't tried all of them).


In 2.9, there is such a conversion, presumably to aid interoperability with Java. (Scala doesn't need it on its own, because it transparently boxes and unboxes primitives, which is perhaps why it wasn't included earlier.)

0

精彩评论

暂无评论...
验证码 换一张
取 消