I want to have a general vector abstract class / trait that specifies certain methods, e.g.:
trait Vec
{
def +(v:Vec):Vec
def *(d:Double):Vec
def dot(v:Vec):Double
def norm:Double
}
I want to have Vec2D
and Vec3D
extend Vec
:
class Vec2D extends Vec { /* implementation */ }
class Vec3D extends Vec { /* implementation */ }
But how can I, for instance, make it so that Vec2D
can only be added to other Vec2D
and not to Vec3D
?
Right now I'm just implementing Vec2D
and Vec3D
without a common Vec
ancestor, but this is getting tedious with duplicate code. I have to implement all my geometry classes that depend on these classes (e.g. Triangle
, Polygon
, Mesh
, ...) twice, once for Vec2D
and again for Vec3D
.
I see the java implementations: javax.vecmath.Vector2d
and javax.vecmath.Vector3d
do not have a common ancestor. What's the reason for this? Is there a way to overcome it in scala?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…