Scala this.type matching the type type of the supertype border parameters

I am 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:

  type arguments [MyClass.this.type] do not conform to method bar type parameter bounds [A <: MyClass] 

Is there anything I can do to make this compilation property using the method signatures above? It seems that MyClass.this.type should be validClass, and I don't want it to be thrown twice when calling the helper (once on the way and once on exit).

+2
source share
1 answer

Singleton types are never inferred. You will have to write

 Helper.bar[this.type](this) 
+4
source

Source: https://habr.com/ru/post/893851/


All Articles