Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the philosophy behind making instance variables public by default in Scala?

Tags:

scala

What is the philosophy behind making the instance variables public by default in Scala. Shouldn't making them private by default made developers make less mistakes and encourage composition?

like image 680
Prathamesh Mone Avatar asked Dec 07 '11 09:12

Prathamesh Mone


1 Answers

First, you should know that when you write:

class Person( val name: String, val age: Int ) {
   ...
}

name and age aren't instance variables but accessors methods (getters), which are public by default.

If you write instead:

class Person( name: String, age: Int ) {
   ...
}

name and age are only instance variables, which are private as you can expect.

The philosophy of Scala is to prefer immutable instance variables, then having public accessors methods is no more a problem.

like image 133
paradigmatic Avatar answered Oct 11 '22 14:10

paradigmatic