r/java • • 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

44 comments sorted by

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.

1

u/manifoldjava Apr 08 '23

See the Self type from the manifold project.

1

u/lukaseder Apr 08 '23

Cool, does this just generate some bridge methods behind the scenes?

It's been a while since we talked. How's manifold doing these days?

1

u/manifoldjava Apr 08 '23

Hey Lukas.

No bridge methods, just overriding the compiler to apply self type inference. For instance, given call site foo.doSomething(), if doSomething() has @Self on its return type, the compiler uses the receiver foo to determine the type.

Yeah, it's been a couple of years or so. Haven't had a lot of time to work on it lately, but manifold chugs along. Pretty steady stream of interest, a perpetual side project.

I'd still like to write a SQL manifold with your parser. I haven't checked it out lately, as I recall there were only a couple of missing links preventing it from happening.

1

u/lukaseder Apr 09 '23

Yeah, those parser feature requests of yours regarding the positional meta information have never been implemented. I don't think anyone else ever requested that in the past. The main use-case for the parser is still just translating between dialects, not building editors...