Jerome ROBERT Jerome ROBERT - 3 years ago 83
Scala Question

constructors in scala (primary/auxiliary/default primary)

A quite simple exercise from Cay Horstmann's book « Scala for the impatient » keeps puzzling me. It's about

default primary
constructors :

ex 5.10 :
Consider the class

class Employee(val name: String, var salary: Double) {
def this() { this("John Q. Public", 0.0) }

Rewrite it to use explicit fields and a default primary constructor.

I'm not sure about I am supposed to do. Could some of you propose a solution ?

However, trying to solve this exercise may have made me aware of something I hadn't noticed before about primary constructor and
fields (as you can see, I'm not quite sure) :

Am I right if I say that a
field (as
in the
class above) may only be initialized through the
constructor and not by an
one ? In the latter case it would be considered by the compiler as a reassignment to a
field causing an error.

At first I thought to
fields as a rough equivalent to final fields in java expecting that it would be legal to assign them for the first time in any constructor but it seems I was wrong.

I am not quite satisfied with what may only be a wild guess so I would appreciate if someone could give me more accurate information about that point.

Answer Source

From "Programming in Scala, 2nd edition" paragraph 6.7:

In Scala, every auxiliary constructor must invoke another constructor of the same class as its first action. The net effect of this rule is that every constructor invocation in Scala will end up eventually calling the primary constructor of the class. The primary constructor is thus the single point of entry of a class.

So all data for initialization of val must be in primary constructor.

Your class with explicit fields may be something like:

class Employee(n: String = "John Q. Public", s: Double = 0.0) {
  val name = n
  var salary = s

or without default parameter values:

class Employee(n: String, s: Double) {
  def this() = this("John Q. Public", 0.0)
  val name = n
  var salary = s
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download