Re: [RFC] switch expression
| From: | Larry Garfield | Date: | Sun, 29 Mar 2020 21:50:24 +0000 |
| Subject: | Re: [RFC] switch expression | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109432@lists.php.net to get a copy of this message | ||
On Sun, Mar 29, 2020, at 4:04 PM, Ilija Tovilo wrote:
> > Having two syntaxes for one keyword is not a good idea,
>
> We're already doing that. What about classes vs anonymous objects?
> Functions vs closures?
> They're using the same keywords. There's no confusion.
>
> Ilija
There's subtle and important differences there.
An anonymous function is still a function, in that it is a routine that takes input and produces
output. A closure is, technically, an anonymous function that has imported variables from its
parent scope.
Anonymous classes (not anonymous objects) are implemented as, legitimately, classes; they just have
an arbitrary internal name. The object that results can be used exactly like any other object, by
design.
I don't believe that's the case here, however.
switch is a language
construct for a *statement*, which branches the flow of control of the program.
What you're proposing is a language construct for an *expression*, which evaluates depending on
internal logic to a different value.
Those are sufficiently distinct that I agree they should have distinct keywords. Plus, the internal
syntax is non-trivial to switch back and forth between (break vs not, etc.), so it is misleading for
people to present them as two slight variants on the same thing; they're really quite distinct,
and that's OK.
My recommendation would be to just borrow Rust's keyword:
$result = match ($var) {
$expression => $expression;
$expression => $expression;
$expression => $expression;
default => $expression;
}
--Larry Garfield