On Tue, Feb 12, 2013 at 7:10 AM, Andrew Phillips
<share...@gmail.com> wrote:
b: B = B@7bc2a4e8
scala> b.x
res2: b.X = 0
scala> println(b.x)
null
The field x, at the bytecode level, is an Object (it was declared in A and inherited.) B specialized its type to Int, but the same storage is used. That means "uninitialized Int" behavior (as opposed to "uninitialized reference" behavior) depends on the contents of the field being unboxed into an Int.
That happens when you call b.x, because x has type Int. That doesn't happen when you println(b.x), because println takes an Any argument, so the expected type of the expression is Any. Scala doesn't understand any need to translate representations, because the expression has been upcast to Any already.
scala> println(b.x: Any)
null
scala> println(b.x: Int)
0