Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69783

Re: Yet Another Switch-Case Syntax Proposal

References <8084-1396540962-768613@sneakemail.com> <CALwzidmw0QbUbNw5PvwmHh+4ghguUhpEnY8TbBZYYyjZkpLBsw@mail.gmail.com> <11366-1396806627-75788@sneakemail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2014-04-06 14:55 -0600
Subject Re: Yet Another Switch-Case Syntax Proposal
Newsgroups comp.lang.python
Message-ID <mailman.8963.1396817791.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Apr 6, 2014 at 11:49 AM, Lucas Malor <3kywjyds5d@snkmail.com> wrote:
> On 3 April 2014 20:12, Ian Kelly ian.g.kelly-at-gmail.com
> |python-list@python.org| <dnl5yyr7ut@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?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Yet Another Switch-Case Syntax Proposal Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-06 14:55 -0600

csiph-web