Totality over generics

Brian Goetz brian.goetz at oracle.com
Fri Sep 24 16:43:40 UTC 2021


Seems unlikely.  Let me make an analogy why.

We chose to do records instead of tuples, for various good reasons.  But of course, records have limitations, and there will be a steady stream of “can we have tuples also” requests.  The answer to all of those is going to be no, because, regardless of whether they would be incrementally useful, having both records and tuples is a recipe for becoming C++/Perl/name your favorite collapsing-under-its-own-weight language.  We made our choice.  

A similar choice is made with sealed classes and union types.  (Yes, there are hints of unions in multi-catch, but even these are better expressed as disjunctions of pattern matches.)  Trying to fill into the holes between sealed classes and union types means we’re stuck in a language with both, which users will then have to understand both individually and in how they interact.  

> On Sep 24, 2021, at 12:19 PM, Per Minborg <minborg at speedment.com> wrote:
> 
> Hi,
> 
> What if we could do something like this in some distant future:
> 
>     <T extends Integer | String> int eval(T t) {
>         return switch (t) {
>             case Integer i -> i;
>             case String s -> Integer.parseInt(s);
>             case null -> 0;
>             // Totality
>         };
>     }
> 
> 
> Already today , an equivalent second layer of sealed wrapper classes could achieve the same goal but with much more ceremony. Of course, there are also overloads but how cool is that?
> 
> Best, Per Minborg
> 



More information about the amber-dev mailing list