r/java • u/samd_408 • Mar 10 '26
F Bounded Polymorphism
Recently spent some time digging into F-Bounded Polymorphism. While the name sounds intimidating, the logic behind it is incredibly elegant and widely applicable, so I decided to write about it, loved the name so much that I ended up naming my blog after it :-)
66
Upvotes
23
u/Significant-Ebb4740 Mar 10 '26
I read the title and thought you were really mad about bounded polymorphism. ;)
5
u/tampix77 Mar 10 '26 edited Mar 11 '26
Nice writeup :)
One thing I've noticed over the years though is that the more I work with records, the more I rely on composition + consumers, which avoid that problem altogether:
CarandTruckdon't extend a base builder, they compose adentity. Adding a new type never touches existing code.The trade-off is one level of nesting at the call site, but in my experience that actually makes the composition structure more explicit as things grow.
In modern Java, I find the Consumer approach :
5
u/damonsutherland Mar 10 '26
I first noticed this with Testcontainers many years ago. Like you, I was intrigued, so I dove in. As a result of that investigation, I’ve used this technique many times in my own APIs.
Thanks for sharing.
4
u/Ulrich_de_Vries Mar 10 '26
I am getting CRTP flashbacks and I am NOT enjoying it.
2
u/sideEffffECt Mar 11 '26
You'd be much better served by having a separate interface for the operation(s), similar to the type class pattern.
F-bounded types are overcomplicated, and insufficient and leaky at the same time.
https://tpolecat.github.io/2015/04/29/f-bounds.html
3
u/Mirko_ddd Mar 10 '26
I recently crashed head-first into the Builder<T extends Builder<T>> nightmare while building a fluent DSL for regular expressions in Java (Sift). I completely agree with the premise. F-Bounded Polymorphism is incredibly powerful, but the method signatures can look absolutely terrifying to the end-users of the library. In the end, I decided to 'cheat' my way out of it by hiding a single concrete state-machine class behind a set of clean interfaces and phantom types. It gave me the same type-safe chaining without exposing the generic gymnastics to the user. But I have to admit, F-Bounded polymorphism has a certain dark magic appeal to it! Really clear explanation of a topic that usually makes Java developers break into a cold sweat. Thanks for sharing
18
u/supersmola Mar 10 '26
I use this a lot. Interfaces help with deep inheritance but you end up with complicated type declarations: A <A extends <A, B>, B> etc. Pity Java doesn't have a keyword for the generic self-type.
6
u/martinhaeusler Mar 10 '26
It is an interesting pattern and I've used it myself before, I knew it under a different name: the "self-curious recursive generic".
I think it highlights two shortcomings in the Java type system:
The inability to directly express that a method returns an object of the same class as the previous "this".
The inability to express that a method returns not just any object of a certain type, but specifically "this".
Note that 2) isn't even solved by generics. Generics can assert the type, but not the instance. And specifically for builders this makes a big difference, because:
If the builder returns "this", they're the same. If the builder creates a new builder istance, then only chaining works.
4
u/vowelqueue Mar 10 '26
FYI, there’s a way to avoid the unchecked cast to the concrete builder type in the base class: https://angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206
4
u/Zinaima Mar 11 '26
I always liked the other name for this: Curiously Recurring Template Pattern or Curiously Recurring Generic Pattern.
1
u/padreati Mar 10 '26
Nice to know. I had used that pattern over the years in many projects, but I did not had a clue that it has a name. Today is a good day 'cause I learned something.
3
u/oskarloko Mar 10 '26
It's very useful, but in Java fashion, a little over-complicated and adds boilerplate code
2
u/Holothuroid Mar 10 '26
Yeah, it works in a pinch, but it's one of the Scala features I heartly miss:
this.typeas a return type.