2

What would be the difference between the following three Scala declarations? I understand the general distinction between val and var.

class A(x: T){ ... }
class B(val x: T){ ... }
class C(var x: T){ ... }
Community
  • 1
  • 1
8128
  • 969
  • 1
  • 8
  • 27

1 Answers1

3

Difference between A and B (both val and var create accessor):

class A(a: Int) {}

// Doesn't compile (value a is not a member of)
// (new A(1)).a

class B(val b: Int) {}

(new B(1)).b                                    //> res0: Int = 1
Victor Moroz
  • 9,167
  • 1
  • 19
  • 23
  • To further clarify, see http://stackoverflow.com/questions/14694712/do-scala-constructor-parameters-default-to-private-val – Jack Viers Aug 13 '13 at 03:02