How do I represent the boxed Double in pure Scala?
Asked Answered
W

2

9

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? ]

Washhouse answered 10/10, 2011 at 21:2 Comment(4)
My guess is that the boxed double is java.lang.Double -- just like Scala uses Java classes for a lot of other things (AnyRef = java.lang.Object, String = java.lang.String). I don't know for sure, though.Moreno
Those statements are true on the JVM. On the CLR there are different mappings, and I want to know how to represent boxed Double on either.Washhouse
@Duncan can you put that as part of the question then? Or even better create a new one. Because running Spring on the CLR will have it's own issues.Whitneywhitson
I've edited the question to make it clear(er) that this is not a question about Spring but rather the Scala type system.Washhouse
W
11

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

Whitneywhitson answered 10/10, 2011 at 21:37 Comment(2)
All these statements are true on the JVM, but Scala targets other environments.Washhouse
From my POV the last section about creating a type alias solves @DuncanMcGregor's problem.Samantha
P
0

Why not creating a bean of type scala.Double? Terrible but seems like it's working:

<bean id="rxAlarmLimitDb" class="scala.Double" factory-method="unbox">
    <constructor-arg>
        <bean class="java.lang.Double">
            <constructor-arg value="4.2"/>
        </bean>
    </constructor-arg>
</bean>
Pomegranate answered 10/10, 2011 at 21:20 Comment(1)
I suppose that my problem is solved with java.Lang.Double - but I'm curious!Washhouse

© 2022 - 2024 — McMap. All rights reserved.