Reducing verbosity for generics

Archie Cobbs archie.cobbs at gmail.com
Sun Jun 9 22:20:13 UTC 2024


On Sat, Jun 8, 2024 at 7:21 PM Sam Carlberg <sam at slfc.dev> wrote:

> The core part of the library is a Unit class with a recursive self type


Side note/digression on "self type"...

I've also wished for a simpler way to create self types that doesn't spam
your entire type hierarchy with a new generic type variable.

For example, instead of this:

public interface Builder<T, B extends Builder<T, B>> {
    B withFoo(Foo f);
    B withBar(Bar b);
    T build();
}

syntactic sugar could allow you to do something like this:

public interface Builder<T> {
    this.class withFoo(Foo f);
    this.class withBar(Bar b);
    T build();
}

The compiler would then assign the return value of any withFoo() invocation
to have the exact same (fully generified) type as the type of the receiver,
and any implementation of withFoo() would just have to return an object
with type compatible with Builder<T>.

I don't see any fundamental reason why this couldn't work but I could be
missing something.

-Archie

-- 
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240609/cd507be2/attachment.htm>


More information about the amber-dev mailing list