Yet Another Switch-Case Syntax Proposal

Ian Kelly ian.g.kelly at gmail.com
Sun Apr 6 16:55:47 EDT 2014


On Sun, Apr 6, 2014 at 11:49 AM, Lucas Malor <3kywjyds5d at snkmail.com> wrote:
> On 3 April 2014 20:12, Ian Kelly ian.g.kelly-at-gmail.com
> |python-list at python.org| <dnl5yyr7ut at sneakemail.com> wrote:
>> Use this instead [of continue]:
>
>>
>> switch day case in ("Mon", "Tue", "Wed", "Thu", "Fri"):
>>     go_to_work = True
>>     day_type = "ferial"
>>     if day in ("Tue", "Thu"):
>>         lunch_time = datetime.time(11, 30)
>>         meeting_time = datetime.time(12, 30)
>>     else:
>>         lunch_time = datetime.time(12)
>>         meeting_time = datetime.time(14)
>> case in ("Sat", "Sun"):
>>     go_to_work = False
>>     day_type = "festive"
>>
>> You get an extra level of indentation this way, but it reads less like
>> spaghetti code.
>
>
> Well, if you have to add an if-else to your switch-case, it means
> switch-case syntax is not so useful.

I agree; the above is better suited to be an if.

> An alternative is to imitate elif, so you'll have elcase. This way we don't
> need continue. Since I do not like elcasein, the best solution is to do as
> suggested by many of you, so case in instead of casein.

So if I'm understanding your intention correctly, "case" means to
check this case regardless of whether any preceding case was matched,
and "elcase" means to check this case only if the most recent "case"
and its dependent "elcases" preceding this one were not matched.

switch "Mon" case in ("Tue", "Thu"):
    print(1)
elcase in ("Mon", "Wed", "Fri"):
    print(2)
case in ("Sat", "Sun"):
    print(3)
elcase in ("Mon", "Tue", "Wed", "Thu", "Fri"):
    print(4)

will print 2 and 4, correct?

> But if you can write case in, why you can't write "case" comp_operator in
> general? With this syntax you can do also case is not, case > etc...

At this point I see no advantage in adding this syntax.  It is so
similar to an if-elif chain that surely any optimization that could be
applied would be equally possible if the if-elif syntax, so why not
just use that?



More information about the Python-list mailing list