> Am 28.04.2020 um 12:48 schrieb Rowan Tommins <rowan.coll...@gmail.com>:
> 
> On Tue, 28 Apr 2020 at 11:19, Ilija Tovilo <tovilo.il...@gmail.com> wrote:
> 
>>> for the control-flow statement, it feels awkward and not in
>>> keeping with the rest of the language. If they were separate proposals,
>> the
>>> statement would probably end up looking very different.
>> 
>> Can you elaborate? If I made a proposal exclusively for match
>> statements the syntax would be exactly equivalent.
>> 
> 
> 
> If we take away the expression part, the proposal would be to replace this
> switch statement:
> 
> switch ( $expr ) {
>     case 1:
>     case 2:
>           statement;
>     break;
>     default:
>           statement;
>     break;
> }
> 
> With this match statement:
> 
> match ( $expr ) {
>     1, 2 => {
>           statement;
>     },
>     default => {
>           statement;
>     },
> }
> 
> The extra set of {} inside looks a little odd, but is a reasonable way to
> remove the implicit fallthrough. However, the other changes seem to just be
> arbitrarily new syntax:
> 
> * No "case" keyword
> * => instead of :
> * commas between statement blocks
> 
> If the motivation is simply to fix the current switch statement, why not
> keep the syntax more familiar?
> 
> match ( $expr ) {
>     case 1, 2: {
>           statement;
>     }
>     default: {
>           statement;
>     }
> }
> 
> The colon looks unnecessary next to the opening brace, but if we keep it,
> the braces could be optional like they are in an if statement:
> 
> match ( $expr ) {
>     case 1, 2: statement;
>     default: statement;
> }
> 
> Note that this wouldn't suffer the problems of accidentally running code
> that you get with one-line if statements, because the following could
> simply be a syntax error:
> 
> match ( $expr ) {
>     case 1, 2: statement; anotherStatement;
>     default: statement;
> }
> 
> 
> That syntax doesn't lend itself as well to being used as an expression, but
> I'm not convinced making one syntax work for both use cases is the right
> goal given the compromises it requires.
> 
> 
> Regards,
> -- 
> Rowan Tommins
> [IMSoP]

Hey Rowan,

I think you should first think about why the "case" needs to exists at all. In 
particular it exists to distinguish goto labels from the case expressions in 
switch. With match, match restricting statements (and thus goto labels) into { 
brackets }, it now is not necessary to do such a distinction and we can get rid 
of the extra token.
"=>" is logical as well, we use the return value of the expressions (see fn() 
and array syntax), the colon is exclusively used for pure statement boundaries.
The comma is necessary for the expression syntax at least (consider match($a) { 
1 => 2  + 2 + 2 => 3 } - is this now match($a) { 1 => 2, (2 + 2) => 3 } or 
match ($a) { 1 => (2 + 2), +2 => 3 } ?) You may argue to make it optional 
behind a statement block though.

Bob
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to