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.
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.
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.
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.
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_=.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With