Self bound generic type with fluent interface and inheritance

社会主义新天地 提交于 2019-11-28 01:52:40

You essentially have a recursive type declaration.

Foo<T extends Foo<T>>.

So let's say you have a Foo<Foo>. That means T is mapped to Foo. But Foo is not subtype of Foo<T>, which in this case is Foo<Foo>, so what you're really looking for is Foo<Foo<Foo>>. But wait a minute, the innermost Foo isn't typed, so I guess it's Foo<Foo<Foo<Foo>>>...oh forget it!

To put a more understandable face on it, consider if you had Foo<T extends List<T>>. What could you possibly use for T in a declaration/instantiation of Foo? List<String>? List<List>?

Edit

It looks like you found a way to "break" the recursion cycle. You eventually need to get to a reified type. In the same way that you found that ConcreteFoo worked for you, you could similarly for the List example above have some class ConreteListOfItself implements List<ConreteListOfItself> that would break the recursion cycle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!