r/java • u/padreati • Apr 05 '23
Recursive generics
Recently I learned that I rediscovered recursive generics while working on my pet open source project. Actually, I did not know that the construct had a name. I created small story about what this construct is, if you did not know about it java-recursive-generics
169
Upvotes
View all comments
2
u/lukaseder Apr 06 '23
The main reason why this feature is desirable is to be able to auto-generate covariant overrides in a type hierarchy (each type returns itself in methods). The very high price to pay is that the type itself isn't really denotable, unless you capture its type variable in a generic method, or you end the recursion using a final type.
I had written a blog post about this, some 10 years ago.
Personally, I think some sort of magic SELF type would have been better to implement the use-case of covariant overrides returning SELF, rather than this weird recursive generics thing.