1

Why don't the following work?

scala> class Foo[B<:Foo[B]]
defined class Foo

scala> class Goo[B<:Foo[B]](x: B)
defined class Goo

scala> class Hoo[B<:Hoo[B]] extends Foo[Hoo[B]] { def f = new Goo(this) }
defined class Hoo

scala> class Ioo extends Hoo[Ioo] { def g = new Goo(this) }
<console>:11: error: inferred type arguments [Ioo] do not conform to class Goo's type parameter bounds [B <: Foo[B]]
       class Ioo extends Hoo[Ioo] { def g = new Goo(this) }
                                            ^

scala> class Ioo extends Hoo[Ioo] { f } // yet this works!
defined class Ioo

1 Answer 1

2

this in new Goo(this) must be B <: Foo[B]. It is Ioo, so we need Ioo <: Foo[Ioo].

Ioo is Hoo[Ioo], hence Foo[Hoo[Ioo]] (inheritance of Hoo), which does not give a Foo[Ioo].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.