Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.Boolean to scala.Boolean question

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)                                                                  
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).

like image 412
George Avatar asked Feb 03 '23 18:02

George


1 Answers

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.)

like image 171
Rex Kerr Avatar answered Feb 05 '23 08:02

Rex Kerr