Scala self-type:类型参数错误的成员 [英] scala self-type: member of type parameter error

查看:136
本文介绍了Scala self-type:类型参数错误的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此问题的后续行动../p>

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

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
                               ^

解决方案

正确的解决方案是:

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
}

针对this question.

Why does this code not compile, and how do I fix it?

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
}

The error is:

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

解决方案

The proper solution is:

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
}

Props to Debilski for the answer to a related question.

这篇关于Scala self-type:类型参数错误的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆