I’m trying to compile the following code:
class MyClass {
def foo(): this.type = Helper.bar(this)
}
class ChildClass extends MyClass
object Helper {
def bar[A <: MyClass](cls: A): A = cls
}
The resulting compiler error is:
type arguments [MyClass.this.type] do not conform to method bar's type parameter bounds [A <: MyClass]
Is there anything I can do to make this compile property with the method signatures provided above? It seems like MyClass.this.type should be a validClass, and I don’t want to have to cast twice when calling Helper (once on the way in and once on the way out).
Singleton types are never inferred. You’ll have to write