Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Scala language require you initialize a instance variable instead of relying on a default value?

Tags:

Scala language requires you initialize your instance variable before using it. However, Scala does not provide a default value for your variable. Instead, you have to set up its value manually by using the wildcard underscore, which acts like a default value, as follows

var name:String = _ 

I know, i know... I can define a constructor in the class definition, which takes as parameter our instance variable, so Scala does not force its initialization as shown below

class Person(var name:String)  

However, i need to declare it in the body because i need to use a Java annotation whose ElementType is FIELD or METHOD; that is, it can just be applied to either a instance variable or method declared in the body of our class.

Question: Why does Scala language require you initialize a instance variable - be it a default value _ or whatever you want - declared in the body of a class instead of relying on a default value ?

like image 791
Arthur Ronald Avatar asked Sep 03 '11 04:09

Arthur Ronald


People also ask

Why is it important to always initialize variables when you declare them?

In addition, when variables are initialized upon declaration, more efficient code is obtained than if values are assigned when the variable is used. A variable must always be initialized before use. Normally, the compiler gives a warning if a variable is undefined. It is then sufficient to take care of such cases.

Why should instance variables be initialized set to a value in the constructor?

I recommend initializing variables in constructors. That's why they exist: to ensure your objects are constructed (initialized) properly.

Are instance variables initialized to a default value?

Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. Values can be assigned during the declaration or within the constructor.

Do instance variables have to be initialized?

Instance variables of numerical type (int, double, etc.) are automatically initialized to zero if you provide no other values; boolean variables are initialized to false; and char variables, to the Unicode character with code number zero. An instance variable can also be a variable of object type.


1 Answers

If you use code like the following you are declaring, that the name should be abstract:

class A {   var name: String } 

I suppose you already knew that. So your question is rather of the syntactical nature. The answer is consistency with other possible abstract candidates.

Suppose you want to do something like this:

class A {    var variable: String = _ // variable has some default value (probably null)    val value: String = _ // value cannot have any default values, since it cannot be reassigned later.    def method: String = _ // method could return some default value (probably null)    type theType = _ // what should the default type be? (Any perhaps?) } 

The last three examples don't even compile. Now suppose you want to do something like this:

class A {    var variable: String    val value: String    def method: String    type theType } 

From my point of view, even someone who barely understands Scala sees only declarations. There is no way to misinterpret them, because there is nothing there else than declarations. The one and only confusion arises when you come from another language and assume for a second that there are some default values. But this confusion is gone as soon as you see the first example (the one with the default values). And btw your class has to be a part of an abstract hierarchy in order to be allowed to declare abstract members, so even if you are new to the language you already get some extra help from the compiler.

I hope this answers your question and happy coding.

like image 192
agilesteel Avatar answered Sep 23 '22 04:09

agilesteel