[Python-ideas] Match statement brainstorm

Manuel Cerón ceronman at gmail.com
Sat May 28 07:07:10 EDT 2016


On Wed, May 25, 2016 at 7:52 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On 25 May 2016 at 15:11, Chris Angelico <rosuav at gmail.com> wrote:
> > On Wed, May 25, 2016 at 3:03 PM, Stephen J. Turnbull <stephen at xemacs.org>
> wrote:
> >> How about
> >>
> >>     for <thing>:
> >>         try <matcher>:
> >>             pass
> >>         try <matcher>:
> >>             pass
> >>
> >> Look Ma!  No new keywords!  Yeah, I know, "for" and "try" both have
> >> very strong connotations in Python already, so this may be a "not even
> >> the Dutch could like it" automatic-parser-only syntax.
> >
> > I'd much prefer a different keyword instead of 'for'. If 'with' hadn't
> > been used, that would be a better choice. Maybe 'using'... or
> > 'switch'. But without iteration, 'for' is going to be very confusing.
>
> As a variant on Guido's original switch/case idea:
>
>     given EXPR [as TARGET]:
>         case MATCH_PATTERN [as TARGET] [and CONDITION]:
>             ...
>         case MATCH_PATTERN [as TARGET] [and CONDITION]:
>             ...
>         case if CONDITION:
>             ...
>         case MATCH_PATTERN [as TARGET]:
>             ...
>         else:


Just a small comment regarding the possible new keyworks:

I very much prefer to use 'given' and 'when' instead of 'switch' and 'case':

 given EXPR [as TARGET]:
        when MATCH_PATTERN [as TARGET] [and CONDITION]:
            ...
        when MATCH_PATTERN [as TARGET] [and CONDITION]:
            ...
        when if CONDITION:
            ...
        when MATCH_PATTERN [as TARGET]:
            ...
        else:

One reason for this is that 'given' and 'when' are less likely to collide
with existing variable names. In fact, there are already variables named
'switch' and 'case' in the standard Python distribution. I haven't found
any for 'given' and 'when'.

Also, given .. when is already used in Perl 5 [1] and 6 [2].

Another alternative is to reuse 'if' instead of 'when'. But I'm not sure if
that would make things very confusing.

[1] http://perldoc.perl.org/perlsyn.html#Switch-Statements
[2] https://doc.perl6.org/language/control#given

Manuel.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160528/b84c305a/attachment.html>


More information about the Python-ideas mailing list