scala self-type:值不是成员错误

发布于 2024-10-13 05:14:35 字数 1041 浏览 1 评论 0原文

这是此问题的后续问题。

我正在尝试使用自类型通过通用超类在 scala 中实现向量:

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
}

这是 3D 向量的实现:

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)
}

不幸的是,这无法编译:

Vec3.scala:10: error: value cross is not a member of Vec[Vec3]
  def perpTo(v:Vec3) = (this.normalize).cross(v.normalize)
                                        ^

出了什么问题,如何修复它?

此外,任何有关自我类型的参考都将受到赞赏,因为我认为这些错误是由于我缺乏理解而出现的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别在捏我脸啦 2024-10-20 05:14:35

为了摆脱所有的麻烦,您必须指定类型参数 VVec 的子类。
现在您可以在任何地方使用 V,因为您的特质知道 V 继承所有 Vec[V] 方法。

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

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

请注意方法 nasty 无法使用 Easy Angel 的方法

To get rid of all the nastiness, you have to specify that the type parameter V is a subclass of Vec.
Now you can just use V everywhere, because your trait knows that V inherits all Vec[V] methods.

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

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

Note the method nasty which won’t compile with Easy Angel’s approach.

抽个烟儿 2024-10-20 05:14:35

我认为,Vec中的方法/应该返回V而不是Vec[V]

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

  def norm:Double = math.sqrt(this dot this)
  def normalize = self / norm
}

方法cross存在于Vec3(或者换句话说,在 V 中),但不在 Vec[V]

I think, that method / in Vec should return V instead of Vec[V]:

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

  def norm:Double = math.sqrt(this dot this)
  def normalize = self / norm
}

method cross exists in Vec3 (or in other words in V) but not in Vec[V]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文