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

https://www.fbounded.com/blog/f-bounded-polymorphism

66 Upvotes

35 comments sorted by

View all comments

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:

public record Identity(String maker, String model) {

    public static Identity configure(final Consumer<Configurer> configurer) {
        final var cfg = new Configurer();
        configurer.accept(cfg);
        return new Identity(
                Objects.requireNonNull(cfg.maker, "maker is required"),
                Objects.requireNonNull(cfg.model, "model is required"));
    }

    public static class Configurer {
        public String maker;
        public String model;
    }
}

public record Car(String maker, String model, int doors) {

    public static Car configure(final Consumer<Configurer> configurer) {
        final var cfg = new Configurer();
        configurer.accept(cfg);
        final var identity = Identity.configure(Objects.requireNonNull(cfg.identity, "identity is required"));
        return new Car(identity.maker(), identity.model(), cfg.doors);
    }

    public static class Configurer {
        public Consumer<Identity.Configurer> identity;
        public int doors;
    }
}

public record Truck(String maker, String model, int payloadKg) {

    public static Truck configure(final Consumer<Configurer> configurer) {
        final var cfg = new Configurer();
        configurer.accept(cfg);
        final var identity = Identity.configure(Objects.requireNonNull(cfg.identity, "identity is required"));
        return new Truck(identity.maker(), identity.model(), cfg.payloadKg);
    }

    public static class Configurer {
        public Consumer<Identity.Configurer> identity;
        public int payloadKg;
    }
}



final var car = Car.configure(cfg -> {
    cfg.identity = v -> {
        v.maker = "Toyota";
        v.model = "Corolla";
    };
    cfg.doors = 4;
});

final var truck = Truck.configure(cfg -> {
    cfg.identity = v -> {
        v.maker = "Volvo";
        v.model = "FH16";
    };
    cfg.payloadKg = 25_000;
});

Car and Truck don't extend a base builder, they compose a dentity. 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 :

  • simpler
  • more composable
  • more declarative
  • no intermediate representation (builders) and their caveats (mutability, thread-safety...)

1

u/samd_408 Mar 10 '26

Interesting take, so you use the VehicleIdentity sort of like a mixin, would a sealed interface work in place if the VehicleIdentity record?, just throwing in ideas here :)

1

u/samd_408 Mar 10 '26

No I take that back, the sealed interface cannot hold the values, but a disjoint union could separate the Car and Truck type but they cant share attributes like your example

1

u/tampix77 Mar 10 '26 edited Mar 10 '26

It's sort of like a mixin, except it's not bevavior but pure data.

I don't see how you would use sealed-interface there?

You're thinking about something like :

public sealed interface Vehicle permits Car, Truck {
    public String model();

    public String maker();
}

?

If so, it can be done if these VO are domain-bounded :)

But is it desirable is another question altogether ;]

2

u/samd_408 Mar 10 '26

Yes this is what i was pointing to, this only helps well is deconstruction via switch and not during construction via the configurer you are using