The leanest classes I've ever seen

That’s right: I’m talking about Scala classes and their incredible conciseness. It’s impossible to look at a class declaration in Scala without appreciating the loathing Scala authors feel for verbosity. Not a single keystroke is wasted in declaring a class like:

scala> class Greetings (var planet: String = "World") {
     |     def hello { println("Hello " + planet) }
     | }
defined class Greetings

scala> val world = new Greetings()
world: Greetings = Greetings@311f56a3

scala> val vulcan = new Greetings("Vulcan")
vulcan: Greetings = Greetings@82f60ce

scala> world.hello
Hello World

scala> vulcan.hello
Hello Vulcan

The Greetings class has one attribute called planet of type String with a default value of “World”. The attribute can be modified during class instantiation, by passing one string to the constructor, like I’ve done for the Vulcan planet. The class features a method called hello which does exactly what you expect: it says “Hello world”. Now, what’s really intriguing of this syntax is its conciseness, its avoidance of every redundancy and verbosity. Scala creates for the planet attribute a pair of getter and setter behind the scenes. The getter is called like the attribute, while the setter has a funny name: planet_=

The reason for this name becomes clear if you know that Scala calls it when you use the attribute on the left hand of an assignment:

scala> vulcan.planet = "Romulus"
vulcan.planet: String = Romulus

scala> println(vulcan.planet)
Romulus

scala> vulcan.planet_=("Remus")

scala> println(vulcan.planet)
Remus

So, I’ve used the implicit call first and the explicit call then. The second one, whilst correct, is never used. It’s the kind of artifact that works better far from the eyes. But every Scalist must know about its existence to declare it in a class where its customization is required:

scala> class Person {
     |     private var privateAge = 0
     |     def age = this.privateAge
     |     def age_=(i: Int) { if (privateAge < i) privateAge = i }
     |     def this(startAge: Int) { this(); this.privateAge = startAge }
     | } 
defined class Person

scala> val p1 = new Person
p1: Person = Person@e26db62

scala> val p2 = new Person(14)
p2: Person = Person@37748ba4

scala> p1.age
res1: Int = 0

scala> p2.age
res2: Int = 14

Here I’ve defined a class name Person which has a private field named privateAge which is accessed through the age getter and the age_= setter. The class has a primary constructor which is implicitly declared and an auxiliary constructor that accepts an integer parameter called startAge which initialize the age of the person. The p1 object has a starting age of 0, as declared by the implicit constructor (private var privateAge = 0). The p2 object instead has a starting age of 14, as specified in the call to the auxiliary constructor. Note that the auxiliary constructor starts by calling the implicit constructor with this().

Now, if I try to assign to a Person an age lower that its current, the assignment is silently discarded:

scala> val keepGrowing = new Person(14)
keepGrowing: Person = Person@5e4c689e

scala> keepGrowing.age = 10
keepGrowing.age: Int = 14

The age_= method avoided setting an age of 10 to a 14 years old person.