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


Groups > comp.lang.python > #69547

Yet Another Switch-Case Syntax Proposal

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <3kywjyds5d@snkmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'programmer': 0.03; 'else:': 0.03; 'syntax': 0.04; 'value,': 0.04; 'elif': 0.05; 'remaining': 0.07; '"if': 0.09; 'default.': 0.09; 'etc).': 0.09; 'executed': 0.09; 'identifier': 0.09; 'pep': 0.09; 'suggest': 0.14; 'random': 0.14; '":"': 0.16; '"if"': 0.16; '::=': 0.16; '["else"': 0.16; 'clause.': 0.16; 'iterable:': 0.16; 'statement.': 0.16; 'statements,': 0.16; 'subject:Case': 0.16; 'suite.': 0.16; 'suite;': 0.16; 'tuple,': 0.16; 'variables,': 0.16; 'exception': 0.16; 'all.': 0.16; 'variable': 0.18; 'bit': 0.19; 'written': 0.21; 'proposed': 0.22; 'case.': 0.24; 'skip': 0.24; 'specify': 0.24; '(or': 0.24; 'solutions.': 0.26; 'switch': 0.26; 'statement': 0.30; "i'm": 0.30; 'code': 0.31; 'usually': 0.31; 'about.': 0.31; 'disabled': 0.31; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'add': 0.35; 'consistent': 0.36; 'keyword': 0.36; 'yield': 0.36; 'next': 0.36; 'should': 0.36; 'example,': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'read': 0.60; 'expression': 0.60; 'tell': 0.60; 'received:unknown': 0.61; 'break': 0.61; 'range': 0.61; 'simply': 0.61; 'first': 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'decided': 0.64; 'more': 0.64; 'statement,': 0.68; 'default': 0.69; 'received:38': 0.81; 'satisfied': 0.81; 'proposal:': 0.84; 'tricky': 0.84; 'subject:Proposal': 0.91
Date Wed, 2 Apr 2014 14:53:19 +0000
To python-list@python.org
Subject Yet Another Switch-Case Syntax Proposal
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
From "Lucas Malor" <3kywjyds5d@snkmail.com>
X-Mailer Perl5 Mail::Internet v
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8816.1396450801.18130.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396450801 news.xs4all.nl 2910 [2001:888:2000:d::a6]:53558
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69547

Show key headers only | View raw


Hi all. I would proposeto you all a switch-case syntax for Python. I already read PEP 3103 and I'm not completely satisfied by any of the proposed solutions. This is my proposal:

switch_stmt ::=  "switch" identifier "case" expression_list ":" suite
    ("case" expression_list ":" suite)*
    ["else" ":" suite]

or, more simply:



switch x case var1:
    ....
case var2:
    ...
case var3:
    ...
else:
    ...



Expression list should yield an iterable. The case suite will be executed if the variable of the identifier is a member of the iterable. 

For example, in a "switch x" statement, the code "case iterable: " is identical to "if x in iterable: " (or elif etc). So if you want to perform the same case block for more than one value, you have only to specify a tuple, a range etc. 
I would suggest to add an exception for non-iterable variables, so that you don't have to write "case var, : " but simply "case var: " and it will be identical to "if x == var: ". It's a bit tricky but the alternative is ugly.

Fallthrough is disabled by default. The continue keyword cause to skip all the remaining current case suite. The next case will be checked. You can't use the continue keyword in the else clause.

Some random remarks:
1. switch is on the same line of the first case. This will avoid any unpythonic syntaxes like:
switch x
case var1:
    ...

or 

switch x:
    case var1:
        ...

2. Fallthrough is disabled by default because IMHO it's what non-programmers expect and that's what programmer usually needs more. I don't think a switch with such a syntax needs a break statement. 

3. As an alternative, the continue statement could be written only at the end of a case suite; it will be less powerful and not consistent with the other compound statements, but it will improve readability.

4. I decided to not use already existing keyword like "if" or "in", since it will be misleading and problematic for syntax highlighters.


Tell me what you think about.

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


Thread

Yet Another Switch-Case Syntax Proposal "Lucas Malor" <3kywjyds5d@snkmail.com> - 2014-04-02 14:53 +0000

csiph-web