Any class parameter is implicitly a class field in Scala:
package ky.example
object Example {
class A(elem: Int)
class B(val elem: Int)
class C(var elem: Int)
}
object Test {
import Example._
val a = new A(3)
val b = new B(3)
val c = new C(3)
a.elem // error: cannot resolve symbol elem
b.elem = 4 // error: reassignment to val
c.elem = 4 // good
}
Note the difference between using val
or var
and not using any keyword at all.
0 comments:
Post a Comment