Reputation: 27375
I wrote the following simple program:
object Main extends App {
println(s"${classOf[MyValueClass].getSimpleName}")
val pt = classOf[MyClass].getDeclaredConstructors.asInstanceOf[Array[Constructor[MyClass]]].iterator.next.getParameterTypes
pt.asInstanceOf[Array[Class[_]]] foreach { c =>
println (c.getSimpleName)
}
}
case class MyValueClass(v: Int) extends AnyVal
class MyClass(v: MyValueClass)
The program prints:
MyValueClass
int
But I expected it would print
MyValueClass
MyValueClass
Why is the parameters of the constructor int
, not MyValueClass
?
Upvotes: 0
Views: 39
Reputation: 14217
because MyValueClass
is extending AnyVal
with only one val Int
parameter.
The type at compile time is Wrapper, but at runtime, the representation is an Int http://docs.scala-lang.org/overviews/core/value-classes.html
ByteCode:
scala> :javap -c MyClass
Compiled from "<console>"
public class $line4.$read$$iw$$iw$MyClass {
public $line4.$read$$iw$$iw$MyClass(int);
Code:
0: aload_0
1: invokespecial #19 // Method java/lang/Object."<init>":()V
4: return
}
as you can see the bytecode, the MyClass
constructor parameter has been dealt as int
primitive type after compiling.
Upvotes: 3