Try to imagine how much code it would take to generate the following in your favorite language:
So, in this small class here are the hidden goodies the developer gets for free:
In Scala the parameter list to a class does a bunch of work. There is a lot about this we can go into but I will likely do another post about it. For now suffice it to say the PRIMARY CONSTRUCTOR generates useful bits for you.
Fields can be used in the class equivalent to
The way the params are defined without the
valmakes the fields object private
For regular method parameters use
valthe reader methods will be generated behind the scenes. For
varthe reader and writer methods will be spun up.
Without additional work you can use the params in methods of the class. This tightens up the code.
- The model and year variables are initialized and become fields on Car.