August 2017
Intermediate to advanced
440 pages
10h
English
The important thing to notice is the fact that if we remove the var/val keyword from the constructor property declaration, we'll end up with a constructor parameter declaration. This means that the property will be changed to a constructor parameter, so no accessors will be generated and we will not be able to access the property on the class instance:
class Fruit(var weight:Double, fresh:Boolean)
val fruit = Fruit(12.0, true)
println(fruit.weight)
println(fruit.fresh) // error
In the preceding example, we have an error because fresh is missing a val or var keyword, so it is a constructor parameter, not a class property such as weight. The following table summarizes the compiler accessor generation: ...
Read now
Unlock full access