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]

Reply via email to