Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setters and Getters in Scala Trait?

Tags:

scala

In Java I would sometimes define an interface for ensuring that classes have "setters" and "getters".

For example, if I have a node in a tree I might define interface such as:

public interface Node { 

    Node getLeft();

    void setLeft(Node node);

    Node getRight();   

    void setRight(Node node);

    int getValue();
}

and then my nodes would implement this interface:

 public Node2D implements Node{
     //implements all of Node's methods along with getters
     // and setters
 }

Would I do the same in Scala, or is it done in a different way?

like image 970
Andriy Drozdyuk Avatar asked Feb 12 '11 18:02

Andriy Drozdyuk


People also ask

What is getters and setters in Scala?

Getter and Setter in Scala are methods that helps us to get the value of variables and instantiate variables of class/trait respectively. Scala generates a class for the JVM with a private variable field and getter and setter methods.

How do you use trait method in Scala?

The trait Equal contain one implemented method that is isEqual() so when user defined class Point extends the trait Equal, implementation to isEqual() method in Point class should be provided. Here it is required to know two important method of Scala, which are used in the following example. obj.

What are the setters and getters?

Getter returns the value (accessors), it returns the value of data type int, String, double, float, etc. For the program's convenience, getter starts with the word “get” followed by the variable name. While Setter sets or updates the value (mutators). It sets the value for any variable used in a class's programs.

Can we create object of trait in Scala?

Traits are similar in spirit to interfaces in Java programming language. Unlike a class, Scala traits cannot be instantiated and have no arguments or parameters. However, you can inherit (extend) them using classes and objects.


2 Answers

Getters and setters are not common in Scala. In fact a var is internally realized with two getter/setter-like methods. If you want the mutable solution (which is usually avoided in Scala if possible), you could simply write

trait Node {
  var left:Node
  var right:Node
  var value:Int
}

class Node2D(var left:Node, var right:Node, var value:Int) extends Node

The Node interface provides little additional value here, and I don't like the idea of vars in it, so I'd probably get rid of the interface alltogether, or would end up with an read-only interface and a mutable class:

trait Node {
  def left:Node
  def right:Node
  def value:Int
}

class Node2D(var left:Node, var right:Node, var value:Int) extends Node

Note that pattern matching makes it much easier in Scala to get back a more specific type (like Node2D from a Node), so don't worry about cases where you would need instanceof in Java.

like image 113
Landei Avatar answered Oct 07 '22 22:10

Landei


In Scala, getters and setters are syntactically identical to vars. So you could declare the variable as a var in your trait:

trait A {
  var x: Int
}

Then you could implement it just as a var, or with getters and setters if you wanted:

class B extends A {
  var _x: Int = 0

  def x: Int = _x

  def x_=(value: Int) {
    println("Setting x to "+value)
    _x = value
  }
}

class C extends A {
  var x = 1
}

Finally, here's how it would look like to use the getters and setters:

val b = new B
b.x // is 0
b.x = 10
b.x // is 10

val c = new C
c.x // is 1
c.x = 20
c.x // is 20

It's worth noting that the preferred design in Scala is to use immutable objects and stick to the functional programming paradigm. See also http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-2.

like image 32
Ankur Dave Avatar answered Oct 07 '22 23:10

Ankur Dave