scala self-type:值不是成员错误
这是此问题的后续问题。
我正在尝试使用自类型通过通用超类在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了摆脱所有的麻烦,您必须指定类型参数
V
是Vec
的子类。现在您可以在任何地方使用
V
,因为您的特质知道V
继承所有Vec[V]
方法。请注意方法
nasty
无法使用 Easy Angel 的方法。To get rid of all the nastiness, you have to specify that the type parameter
V
is a subclass ofVec
.Now you can just use
V
everywhere, because your trait knows thatV
inherits allVec[V]
methods.Note the method
nasty
which won’t compile with Easy Angel’s approach.我认为,
Vec
中的方法/
应该返回V
而不是Vec[V]
:方法cross存在于
Vec3
(或者换句话说,在V
中),但不在Vec[V]
中I think, that method
/
inVec
should returnV
instead ofVec[V]
:method cross exists in
Vec3
(or in other words inV
) but not inVec[V]