Composition of pattern questions

Brian Goetz brian.goetz at oracle.com
Thu Mar 4 17:13:52 UTC 2021


While this example:

>
>     switch (ignored) {
>         case true(x > 3): A
>         case true(x < 3 && y > 0): B
>         default: C
>     } 

may feel contrived, there are other similar examples that are far less 
contrived.  For example:

     switch (target) {
         case Foo(Bar x): A
         case Foo(Baz x): B
         case Foo(var x): C
         case true(configFooOnly): break;
         case Quux q: D
         ...
    }

This amounts to an if-else chain where we handle all the Foos, and then, 
based on some non-target criteria, decide whether to handle other 
things, or just break out.  Having one non-target criteria in a switch 
would otherwise mean you have to refactor the whole switch to an if-else 
chain, which would suck!

This is a reasonable argument for (at least eventually) not restricting 
where true/false patterns can appear.


More information about the amber-dev mailing list