[pattern-switch] Totality

Guy Steele guy.steele at oracle.com
Fri Aug 28 22:49:26 UTC 2020


> On Aug 28, 2020, at 5:59 PM, forax at univ-mlv.fr wrote:
> 
> . . .
> Again, it should work like a cascade of if ... instanceof, so
>   case Pixel(var x, var y, var color) -> color
> should be equivalent to
>   if x instanceof Pixel p { yield p.color() }

But I do not believe that at all.  I do believe that

  case Pixel(var x, var y, var color) -> color

should be equivalent to

  if x instanceof Pixel(var x, var y, var color) p { yield p.color() }

or, if you prefer, to

  if x instanceof Pixel(var x, var y, var color) { yield color }

The point is that the switch label `case Pixel(var x, var y, var color)` does not merely demand that the selector value be a Pixel; it demands that it be a Pixel having a specific three-argument destructor.  It can be equivalent only to an instanceof expression that makes those same demands.

If you want a switch clause that is equivalent to

  if x instanceof Pixel p { yield p.color() }

then you should write

  case Pixel p -> p.color()



More information about the amber-spec-experts mailing list