[pattern-switch] Totality

Remi Forax forax at univ-mlv.fr
Fri Aug 28 01:13:38 UTC 2020


> De: "Brian Goetz" <brian.goetz at oracle.com>
> À: "Guy Steele" <guy.steele at oracle.com>
> Cc: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>,
> "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Mercredi 26 Août 2020 19:01:16
> Objet: Re: [pattern-switch] Totality

> I think we now have a sound story for totality in both patterns and switches.
> Let's start with refining what we mean by totality.

> We have seen a lot of cases -- and not just those involving enums or sealed
> types -- where we want to say a pattern or set of patterns is "total enough" to
> not force the user to explicitly handle the corner cases. In these cases, we
> will let the compiler handle the corner cases by generating exceptions.

> A prime example is the deconstruction pattern Foo(var x); this matches all Foos,
> but not null. Similarly, there is a whole family of such corner cases:
> Foo(Bar(var x)) matches all Foos, except for null and Foo(null). But we are in
> agreement that it would be overly pedantic to force the user to handle these
> explicitly.
While i agree with the general idea, i disagree with the fact that the compiler should handle the null and total cases, it can not handle the total case, it has to be handled by the by the runtime, not the compiler. 

I will use '?' for the total type, by example this switch is "total enough" 
sealed interface Stuff permits Pixel, Car {} 
record Pixel(int x, int y, Color color) implements Stuff {} 
record Car(Color color) implements Stuff {} 
switch(stuff) { 
case Pixel(? x, ? y, Color color) -> color; 
case Car(Color color) -> color 
} 

We have agreed during one of our first meetings that we want that if Pixel is changed to record Pixel(BigInteger x, BigInteger y, Color color) {} with or without recompilation of the switch it should be Ok. 
So here, '?' can not be 'var', at least if we want var to be only the inferred type. 

In term of semantics, there is a difference between var which is an implicit type and '?' which represents whatever type. 

That doesn't mean that '?' can not be spelt 'var', but it demonstrates that the corner cases should be managed by the runtime, not the compiler. 

Rémi 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20200828/fa71a7cf/attachment.htm>


More information about the amber-spec-experts mailing list