How do I present a Double insert in pure Scala?

Scala has 2 double precision numbers, one AnyVal , the other AnyRef . On the JVM, they are mapped to a primitive double and class java.lang.Double respectively.

Now, what happens on platforms other than the JVM? I can use Scala.Double for the primitive, but how can I indicate that I want a Double insert reference without specifying java.lang.Double ?


[The context is on the left to understand the response of Thomas, but not the fundamental problem.

I have a Double that I want to insert using Spring into the Wicket component:

 class MyPanel(id: String) extends Panel(id) { @SpringBean(name="rxAlarmLimitDb") var alarmLimitDb: Double = _ 

If I specify the type as Scala.Double , as indicated above, the injector fails because it can only inject objects.

If I specify java.lang.Double as the field type, everything is fine

 class MyPanel(id: String) extends Panel(id) { @SpringBean(name="rxAlarmLimitDb") var alarmLimitDb: java.lang.Double = _ 

But I'm trying to reduce my dependency on rejecting the Java API, so how can I imagine a double in a box without it? ]

+7
source share
2 answers

scala.Double == double in Java. When you insert a scala.Double , it becomes 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 is no way to create an object of type scala.Double . This is just an alias for two. So for your problem you need to use java.lang.Double or wrap it in your own type and provide implicit conversions.

This definition makes sense if you think about it. All interaction between java and scala code, which requires autoboxing and unpacking, will work properly.

If that matters, you can always:

 type BoxedDouble = java.lang.Double 

then you will not need to see java.lang :-)

+9
source

Why not create a bean of type scala.Double ? Awful, but it looks like it works:

 <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> 
0
source

All Articles