I am looking for a way to define a method that returns a type T, where T = the type of the subclass.
I know I can do this using abstract types, but I don't like the overhead of having to override T for each subclass.
Code example:
object Helper { def help[A <: MyClass](cls: A): Option[A] = { cls.foo() map { _.asInstanceOf[A] } } } class MyClass { type T <: MyClass def foo(): Option[T] = Some(this.asInstanceOf[T]) } class ChildClass extends MyClass { type T = ChildClass }
Perhaps a new language feature made it easier? Or can I somehow use this.type? It is important for me that I can define a helper class that can call foo this way.
source share