In fact, using Option less cumbersome, since the question of whether value was initialized or not can be deduced from the value of Some or None in Option . This is more idiomatic Scala than using flags.
class X() { var value: Option[Int] = None def f(param: Int) { value match{ case None => value = Some(param) case Some(s) => println("Value already initialized with: " + s) } } } scala> val x = new X x: X = X@6185167b scala> xf(0) scala> x.value res1: Option[Int] = Some(0) scala> xf(0) Value already initialized with: 0
Brian source share