Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is exactly the point of auto-generating getters/setters for object fields in Scala?

Tags:

scala

As we know, Scala generates getters and setters automatically for any public field and make the actual field variable private. Why is it better than just making the field public ?

like image 427
Michael Avatar asked Nov 26 '10 07:11

Michael


3 Answers

For one this allows swapping a public var/val with a (couple of) def(s) and still maintain binary compatibility. Secondly it allows overriding a var/val in derived classes.

like image 89
Silvio Bierman Avatar answered Nov 15 '22 20:11

Silvio Bierman


First, keeping the field public allows a client to read and write the field. Since it's beneficial to have immutable objects, I'd recommend to make the field read only (which you can achieve in Scala by declaring it as "val" rather than "var").

Now back to your actual question. Scala allows you to define your own setters and getters if you need more than the trivial versions. This is useful to maintain invariants. For setters you might want to check the value the field is set to. If you keep the field itself public, you have no chance to do so.

This is also useful for fields declared as "val". Assume you have a field of type Array[X] to represent the internal state of your class. A client could now get a reference to this array and modify it--again you have no chance to ensure the invariant is maintained. But since you can define your own getter you can return a copy of the actual array.

The same argument applies when you make a field of a reference type "final public" in Java--clients can't reset the reference but still modify the object the reference points to.

On a related note: accessing a field via getters in Scala looks like accessing the field directly. The nice thing about this is that it allows to make accessing a field and calling a method without parameters on the object look like the same thing. So if you decide you don't want to store a value in a field anymore but calculate it on the fly, the client does not have to care because it looks like the same thing to him--this is known as the Uniform Access Principle

like image 45
weberste Avatar answered Nov 15 '22 19:11

weberste


In short: the Uniform Access Principle.

You can use a val to implement an abstract method from a superclass. Imagine the following definition from some imaginary graphics package:

abstract class circle {
  def bounds: Rectangle
  def centre: Point
  def radius: Double
}

There are two possible subclasses, one where the circle is defined in terms of a bounding box, and one where it's defined in terms of the centre and radius. Thanks to the UAP, details of the implementation can be completely abstracted away, and easily changed.

There's also a third possibility: lazy vals. These would be very useful to avoid recalculating the bounds of our circle again and again, but it's hard to imagine how lazy vals could be implemented without the uniform access principle.

like image 37
Kevin Wright Avatar answered Nov 15 '22 20:11

Kevin Wright