In Scala there are 2 representations of double-precision numbers, one is an AnyVal
, the other AnyRef
. On the JVM they are mapped to the primitive double
and the class java.lang.Double
respectively.
Now what happens on platforms other than the JVM? I can use Scala.Double
for the primitive, but how do I specify that I want a reference to the boxed Double without specifying java.lang.Double
?
[Context - left to make sense of Thomasz' answer, but not the fundamental issue.
I have a Double that I want to inject with Spring into a Wicket component:
class MyPanel(id: String) extends Panel(id) {
@SpringBean(name="rxAlarmLimitDb") var alarmLimitDb: Double = _
If I specify the type as scala.Double
as above, the injector fails, as it can only inject Objects.
If I specify java.lang.Double
as the type of the field, all is well
class MyPanel(id: String) extends Panel(id) {
@SpringBean(name="rxAlarmLimitDb") var alarmLimitDb: java.lang.Double = _
But I'm trying to reduce my dependance on falling back on the Java API, so how do I represent the boxed Double
without it?
]
scala.Double
== double
in Java. When you box a scala.Double
, it becomes a java.lang.Double
.
scala> val f = 45d;
f: Double = 45.0
scala> println("f=" + f.getClass)
f=double
scala> def foo(d: Any) = println("d=" + d.getClass)
foo: (d: Any)Unit
scala> foo(f)
d=class java.lang.Double
There isn't any way of creating an object of type scala.Double
. It's just an alias for double. So for your problem, you need to use a java.lang.Double
, or enclose it in your own type and provide implicit conversions.
This definition makes sense if you think about it. All of the interaction between java & scala code which requires autoboxing & unboxing will work as expected.
If it makes a difference, you can always do:
type BoxedDouble = java.lang.Double
then you won't have to see the java.lang :-)
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