Re: [RFC] switch expression
| From: | Ilija Tovilo | Date: | Sun, 29 Mar 2020 22:07:52 +0000 |
| Subject: | Re: [RFC] switch expression | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109436@lists.php.net to get a copy of this message | ||
Hi Larry
Thanks for your suggestion.
I chose to use switch instead of match for a couple of reasons:
1. It uses the same AST, code generation and opcodes as the switch, I
don't agree that it is significantly different than the switch that we
already have.
2. Adding the
match keyword is a breaking change, not an insignificant one.
3. If we can fix the switch statement semantics in the future the two
will only differ in that one returns a value and the other one
doesn't. This is a much smaller distinction than functions/closure.
4. If we'd every want to add pattern matching, we'd still have a
keyword available to us.
> I don't believe that's the case here, however. Àà™-ÒøAÐÏ
> Õβ—Ù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.
Well, I encourage you to look at the implementation. You'll see that
the two are in fact very similar.
I'm not averse to using the match keyword if people are ok with the BC
change. If we do though we definitely should make it work as a
statement, allow blocks in addition to single expression and also fix
type coercion. This would make it a replacement of the switch
statement instead of an addition.
Ilija