[Python-ideas] Yet Another Switch-Case Syntax Proposal

Lucas Malor 7vsfeu4pxg at snkmail.com
Tue Apr 22 19:37:30 CEST 2014


On 22 April 2014 18:59, Bruce Leban bruce-at-leapyear.org
|python-ideas-at-python.org| <vajg1g2cqt at sneakemail.com> wrote:
> My point is that this Case class does what you want with no language
> changes.

This is not correct. As I already said, I _would_ have a switch
statement that is simpler to code than an if-elif chain. On the
contrary your class creates code that is more complicated than an
if-elif chain.

Furthermore your class implements comparators other than "in" and
"==", and this is not my goal. I quote what I wrote before:
> I also thought about a syntax like this:
> "case" comparator case_expr ":"
> but IMHO it's too verbose for the typical uses of switch. If you want
> flexibility, you can always use if-elif.

And about simplicity, if I sacrifice the fallback and make the "break"
the default and unique behaviour, I don't need "elcase" and my syntax
will be more easy than before:

switch tarot case 0:
    card = "Fool"
case 1:
    card = "Alan Moore"
case 2:
    card = "High Priestess"
<etc....>

I think this is less complicated to read and it's more practical,
since usually you want to "break". If you don't want to "break", you
can create another switch.
Probably I have to not use "switch" and "case", since it seems a C
switch, while its behaviour is completely different now (C "case"
falls back by default and can break, while this "case" breaks by
default and can't fall back). Maybe something like dispatch-case, or
inspect-case.


More information about the Python-ideas mailing list