NoIdeaHowToFixThis
NoIdeaHowToFixThis

Reputation: 4564

chain of subtypings - just a syntax limitation?

This question has likely been already been asked.

Why is this not supported?

class A;
class B[T];
class MyClass[T <: B[U <: A]];

I could have shrugged this off as some limitation of the language syntax but now I am afraid there is a somewhat "deeper" explanation.

class A;
class B[T];
class MyClass[U <: A, T <: B[U]];

Upvotes: 4

Views: 33

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170733

Nested type bounds like this are supported. If you don't want U to be a parameter of MyClass, you can write

class MyClass[T <: B[_ <: A]]

class C extends A
class D extends B[C]

def x: MyClass[D] = ??? // compiles
def y: MyClass[B[C]] = ??? // compiles
def z: MyClass[B[String]] = ??? // doesn't compile because `String` is not a subtype of `A` 

Does this correspond to what you want?

Upvotes: 2

Related Questions