I want to convert F bounded polymorphism to abstract type members.
trait FBoundedMovable[Self <: FBoundedMovable[Self]] {
def moveTo(pos: Vect2): Self
}
to
trait Movable { self =>
type Self <: (Movable { type Self = self.Self })
def moveTo(pos: Vect2): Self
}
So far so good.
Lets define an instance:
case class Ship(pos: Vect2) extends Movable {
type Self = Ship
def moveTo(pos: Vect2) = copy(pos = pos)
}
And try to use it:
// [error] found : a.Self
// [error] required: A
def move[A <: Movable](a: A, to: Vect2): A = a.moveTo(to)
F bounded version works fine.
def moveF[A <: FBoundedMovable[A]](a: A, to: Vect2): A = a.moveTo(to)
I know it's possible to add type bounds to method definition site:
def move2[A <: Movable { type Self = A }](a: A, to: Vect2): A = a.moveTo(to)
But is it possible to specify the relationship in Movable trait declaration? If not - why?
Edit 1
I've realised what problem I am having.
Lets say we want to declare that something is a unit in our world.
trait WorldUnit extends Movable with Damageable
All units are movable and damagable.
Our combat calculation stuff only cares that stuff is Movable with Damagable
. It doesn't care whether it is unit or building.
However we can have code like this:
def doCombat(obj: Movable with Damagable) = obj.moveTo(...).takeDamage(...)
def doStuffWithUnit(obj: WorldUnit): WorldUnit = doCombat(obj) // and the type is lost here.
Am I doomed for F bounded types?
Edit 2:
The question is not answered by Attempting to model F-bounded polymorphism as a type member in Scala - I've tried that before and it doesn't impact the return type in a slightest, it's still a.Self.
Edit 3:
I've found http://blog.jessitron.com/2014/02/when-oo-and-fp-meet-mytype-problem.html but a problem is still unresolved.
Basically, whenever you have a collection and want to pick one:
(collection: Seq[Movable]).collectFirst { m: Movable if m.someCondition => m }
- you have no way of specifying the type bound, thus compiler cannot prove that A#Self =:= A?