Reputation: 71
Here is the class along with trait that it extends to:
class Atom(var x: Double, var y: Double, var z: Double) extends AtomTrait{
private var name: String = null
private var coords = Vector(this.x,this.y,this.z)
def setCoords(x: Double, y: Double, z: Double){
this.x = x
this.y = y
this.z = z
}
def getCoords = coords
def setName(name: String){ this.name = name }
def getName = this.name
}
trait AtomTrait {
def getCoords
def setCoords
def setName(name: String)
def getName: String
}
I get an error of class Atom needs to be abstract, since method setCoords in trait AtomTrait of type => Unit is not defined
I thought my setCoords
method returns Unit since it only assigns values to private variables. What am I doing wrong?
Upvotes: 1
Views: 1221
Reputation: 144136
The signature for setCoords
should be:
trait AtomTrait {
def setCoords(x: Double, y: Double, z: Double)
}
If you need to different coordinate types you can make the trait generic:
trait AtomTrait[C] {
def getCoords: C
def setCoords(coords: C)
}
class Atom(var x: Double, var y: Double, var z: Double) extends AtomTrait[(Double, Double, Double)] {
def setCoords(coords: (Double, Double, Double)) {
coords match {
case (x, y, z) => {
this.x = x
this.y = y
this.z = z
}
}
}
def getCoords = (x, y, z)
}
Upvotes: 3