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).
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With