Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala getters/setters - best practice?

I'm Java SE/EE developer, but beginner in Scala. In Java, when I have some private fields which should be accessible to other code, I use getX()/setX() classic style of getters/setters. However, not sure how about Scala. I noticed that in Scala, naming convention of getter/setter for field is to use the same name as the field's one. So is it OK to simply set the field public, or should I use this style of getter/setter?:

private var _value = .....
def value = _value
def value_= (newVal:Int) = _value = newVal

Is it OK (according to scala naming convention) to put underscore before the field name itself?

Thank you.

like image 829
xwinus Avatar asked Sep 24 '14 08:09

xwinus


People also ask

What are getter and setter methods in Scala?

Scala generates a class for the JVM with a private variable field and getter and setter methods. In Scala, the getters and setters are not named getXxx and setXxx, but they are used for the same purpose. At any time, we can redefine the getter and setter methods ourself. Setter are a technique through which we set the value of variables of a class.

Should you use Getter and setter methods in your interface?

That’s why people keep saying you should use getter and setter methods. This good practice can become irrelevant when you use a language that can hook getter and setter methods to the existing interface. So, you don’t have to pollute your code by writing getter and setter methods explicitly right from the get-go.

What is the use of Getter and setter in Python?

It sets the value for any variable which is used in the programs of a class. and starts with the word “set” followed by the variable name. Getter and Setter make the programmer convenient in setting and getting the value for a particular data type.

How to define a public age field in Scala?

Here, we define a public field: Scala generates a class for the JVM with a private age field and getter and setter methods. These methods are public because we did not declare age as private. (For a private field, the getter and setter methods are private.) In Scala, the getter and setter methods are called age and age_=.


Video Answer


1 Answers

The Scala Style Guide covers this quite nicely.

For accessors of properties, the name of the method should be the name of the property.

Scala does not follow the Java convention. Scala promotes the view that a caller should not be able to tell the difference between a field access and a method call, which means that the convention is to give them both the same name; thus reducing the amount of code change required should a field be changed to a method or visa versa.

Is it OK (according to scala naming convention) to put underscore before the field name itself?

Scala convention is to prefix fields that we want to be private that otherwise have the same name as a public method, or to postfix it with a zero. Either approach is acceptable.

private var _value = .....
def value = _value
def value_= (newVal:Int) = _value = newVal

However, given this example the extra lines are not necessary. The convention exists so that we can use this shorter version and then change it later to the more explicit version when/if we need to without having to make changes at every call site.

var value:Int = 0
like image 136
Chris K Avatar answered Sep 19 '22 08:09

Chris K