Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Constructor Parameters

Tags:

What is the difference between a private var constructor parameter and a constructor parameter without val/var? Are they same in terms of scope/visibility?

Ex:

class Person(private var firstName:String, lastName:String) 
like image 862
user2456976 Avatar asked Sep 04 '13 21:09

user2456976


People also ask

Does Scala have constructor?

Scala ConstructorThere are two types of constructor in Scala – Primary and Auxiliary. Not a special method, a constructor is different in Scala than in Java constructors. The class' body is the primary constructor and the parameter list follows the class name. The following, then, is the default primary constructor.

What is primary constructor in Scala?

The primary constructor can have zero or more parameters. The parameters of parameter-list are declared using var within the constructor then the value could change. Scala also generates getter and setter methods for that field.

What is primary and auxiliary constructor in Scala?

A scala class can contain zero or more auxiliary constructor. The auxiliary constructor in Scala is used for constructor overloading and defined as a method using this name. The auxiliary constructor must call either previously defined auxiliary constructor or primary constructor in the first line of its body.

Can trait in Scala have constructor?

We can extend from multiple traits, but only one abstract class. Abstract classes can have constructor parameters, whereas traits cannot.


1 Answers

Yes, there are two important differences. First for the easy one: constructor parameters without the var or val keywords are not mutable variables—their values can't be changed in the body of the class.

Even if we restrict ourselves to the val keyword, though, there's still a difference between private val and keyword-less parameters. Consider the following:

class Person(private val firstName: String, lastName: String) 

If we look at the compiled class with javap -v Person, we'll see that it only has one field, for firstName. lastName is just a constructor parameter, which means it may be garbage-collected after the class is initialized, etc.

The compiler is smart enough to know when the value of lastName will be needed after initialization, and it will create a field for it in that case. Consider the following variation:

class Person(private val firstName: String, lastName: String) {   def fullName = firstName + " " + lastName } 

The compiler can tell that it may need the value of lastName later, and if we check javap again we'll see that the class has two fields (note that if we'd defined fullName as a val instead of a def, it'd only have one field).

Lastly, note that if we make firstName object-private instead of class-private, it works exactly like a plain old keyword-less constructor parameter:

class Person(private[this] val firstName: String, lastName: String) 

This works even with var instead of val:

class Person(private[this] var firstName: String, lastName: String) 

Both of these classes will have no fields. See section 5.2 of the language specification for more details about object-private access.

like image 53
Travis Brown Avatar answered Sep 20 '22 14:09

Travis Brown