Scala: implementing method with return type of concrete instance
问题 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() method, and I'm currently using an approach based on abstract types: abstract class A(id: Int) { type Self <: A def copy(newId: Int): Self } class B(id: Int, x: String) extends A(id) { type Self = B def copy(newId: Int) = new B(newId, x) } class C(id: Int, y: String, z: String) extends A(id) { type Self = C def copy(newId: Int) =