This is a followup to this question.
I'm trying to implement vectors in scala with a generic super class using self-types:
trait Vec[V] { self:V =>
def /(d:Double):Vec[V]
def dot(v:V):Double
def norm:Double = math.sqrt(this dot this)
def normalize = self / norm
}
Here's an implementation of a 3D vector:
class Vec3(val x:Double, val y:Double, val z:Double) extends Vec[Vec3]
{
def /(d:Double) = new Vec3(x / d, y / d, z / d)
def dot(v:Vec3) = x * v.x + y * v.y + z * v.z
def cross(v:Vec3):Vec3 =
{
val (a, b, c) = (v.x, v.y, v.z)
new Vec3(c * y - b * z, a * z - c * x, b * x - a * y)
}
def perpTo(v:Vec3) = (this.normalize).cross(v.normalize)
}
Unfortunately this doesn't compile:
Vec3.scala:10: error: value cross is not a member of Vec[Vec3]
def perpTo(v:Vec3) = (this.normalize).cross(v.normalize)
^
What's going wrong, and how do I fix it?
Additionally, any references on self-types would be appreciated because I think these errors are cropping up from my lack of understanding.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…