Tutorial by Examples

var A var is a reference variable, similar to variables in languages like Java. Different objects can be freely assigned to a var, so long as the given object has the same type that the var was declared with: scala> var x = 1 x: Int = 1 scala> x = 2 x: Int = 2 scala> x = "foo...
lazy val is a language feature where the initialization of a val is delayed until it is accessed for the first time. After that point, it acts just like a regular val. To use it add the lazy keyword before val. For example, using the REPL: scala> lazy val foo = { | println("Initial...
You can overload a def if the signature is different: def printValue(x: Int) { println(s"My value is an integer equal to $x") } def printValue(x: String) { println(s"My value is a string equal to '$x'") } printValue(1) // prints "My value is an integer equal ...
When invoking a def, parameters may be assigned explicitly by name. Doing so means they needn't be correctly ordered. For example, define printUs() as: // print out the three arguments in order. def printUs(one: String, two: String, three: String) = println(s"$one, $two, $three") ...

Page 1 of 1