slouc slouc - 10 months ago 55
Scala Question

Scala recursive val behaviour

What do you think prints out?

val foo: String = "foo" + foo

val foo2: Int = 3 + foo2



Why? Is there a part in specification that describes/explains this? (I mean foo being
on the right hand side of first assignment and the mechanism not having the same behaviour in the second example)

Answer Source

Yes, see the SLS Section 4.2.

foo is a String which is a reference type. It has a default value of null. foo2 is an Int which has a default value of 0.

In both cases, you are referring to a val which has not been initialized, so the default value is used.