2011-01-23 129 views
2

这是this question的后续行动。斯卡拉自我类型:成员类型参数错误

为什么此代码不能编译,我该如何解决?

trait Vec[V] { self:V => 
    def -(v:V):V 
    def dot(v:V):Double 

    def norm:Double = math.sqrt(this dot this) 
    def dist(v:V):Double = (this - v).norm 
} 

的错误是:

Vec.scala:6: error: value norm is not a member of type parameter V 
    def dist(v:V):V = (this - v).norm 
          ^

回答

3

正确的解决方案是:

trait Vec[V <: Vec[V]] { self:V => 
    def -(v:V):V 
    def dot(v:V):Double 

    def norm:Double = math.sqrt(this dot this) 
    def dist(v:V):Double = (this - v).norm 
} 

道具Debilski的答案到related question

6

通过改变的定义 - 以

def -(v:V):Vec[V] 
+0

谢谢。出了什么问题? – dsg 2011-01-23 21:23:21