Pattern matching for switch (Third Preview)
Brian Goetz
brian.goetz at oracle.com
Wed Mar 23 20:38:26 UTC 2022
> Btw, I think it would be nice to have less keywords, so what about reusing `if` instead of introducing the new contextual keyword `when`? This should probably help with parsing (and since `if` works only in statements, this is probably unambiguous both for the compiler and to a human).
This was discussed. The primary argument in favor of this is "keyword
reuse", but keyword reuse is often a false economy, and people are
inclined to over-rotate on this.
Some of the downsides were:
>
> - The generality of "if" reminded people of the Perl-style "statement
> unless condition" postfix convention, and that people might see it as
> an "inconsistency" that they could not then say
>
> x = 3 if (condition);
>
> which is definitely somewhere we don't want to go.
>
>
> - We're use to seeing "if" with a consequence, and a "naked" if might
> have the effect of "lookahead pollution" in our mental parsers.
>
>
> - Keeping `if` for statements allows us to keep the "body" of case
> clauses visually distinct from the "envelope":
>
> case Foo(var x)
> if (x > 3) : if (x > 10) { ... }
>
> would make people's eyes go buggy. One could argue that "when" is not
> fantastically better:
>
> case Foo(var x)
> when (x > 3) : if (x > 10) { ... }
>
> but it doesn't take quite as long to de-bug oneself in that case.
To which I'll add: `when` is more in line with what other languages with
a similar construct do.
More information about the amber-dev
mailing list