I need a way to enforce a method in an abstract class to have a return type of the concrete class of the object it is called on. The most common example is a copy
Do not force the type bound on the declaration side, unless you need that bound within the definition of A
itelf. The following is sufficient:
abstract class A(id: Int) {
type Self
def copy(newId: Int): Self
}
Now force the Self
type on the use site:
def genNewId(): Int = ???
def createCopies[A1 <: A { type Self = A1 }](seq: Seq[A1]): Seq[A1] =
seq.map(_.copy(genNewId()))