scala self-type: value is not a member error

[亡魂溺海] 提交于 2019-11-28 13:12:33
Debilski

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.

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]

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!