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

Andrew Barnert abarnert at yahoo.com
Tue Apr 22 19:39:32 CEST 2014


On Apr 22, 2014, at 8:01, "Lucas Malor" <7vsfeu4pxg at snkmail.com> wrote:

> On 20 April 2014 04:17, Bruce Leban bruce-at-leapyear.org |python-ideas-at-python.org| <vajg1g2cqt at sneakemail.com> wrote:
>> 
>> I found that the ugliest part of your proposal since it hides the first case and at the same time makes it appear special when it isn't.
> 
> Myabe yes, but if you "translate" it in an if-elif, also the if seems to be "special".

Yes, there is a difference, but it's not nearly as dramatic as with your version. The conditions still look roughly parallel, only one short word away from the indent point, instead of the first one being way off to the right.

On Apr 22, 2014, at 8:27, "Lucas Malor" <7vsfeu4pxg at snkmail.com> wrote:

> Sorry, I didn't read it carefully. Anyway, youe example can be written as:
> 
> for i in range(5):
>     print(i, end=' => ')
>     if i == 1:
>         print('one')
>     elif i == (2,3):
>         print('tuple(two, three)')
>     elif i in (2, 3):
>         print('two or three')
>     elif i > 3:
>         print('more than three')
>     else:
>         print('unmatched')
> 
> and it's much simpler to read.

That's pretty much his point. The same can be said for all of your examples.

Your version is only "simpler" in that your "case" and "elcase" are spelled "if case" and "elif case" in his. Meanwhile, your version is more complex in that the first case is crammed on the same line with the "switch". Also, instead of automatically handling tuples in a natural and obvious way, you're forced to invent a new builtin class and a new display form just to distinguish tuples inline in a case label, and still can't easily handle the distinction in a natural and readable way. And, while his can be trivially extended to handle other types of comparison besides == and in, yours can't. So, you've come up with something that's more complex, less natural, and less powerful than what we can already do, and the only benefit is a little bit of brevity.

> I suggested the switch statement for a simpler alternative to the if-elif chain.
> 
> 
> 
> On 22 April 2014 17:15, Chris Angelico rosuav-at-gmail.com |python-ideas-at-python.org| <iv1yq22odt at sneakemail.com> wrote:
>> On Wed, Apr 23, 2014 at 1:01 AM, Lucas Malor <7vsfeu4pxg at snkmail.com> wrote:
>> >> for i in range(5):
>> >>     print(i, end=' => ')
>> >>     case = Case(i)
>> >>     if case(1):
>> >>         print('one')
>> >>     elif case((2,3)):
>> >>
>> >>         print('tuple(two, three)')
>> >>
>> >>     elif case(2, 3):
>> >>         print('two or three')
>> >>     elif case > 3:
>> >>         print('more than three')
>> >>
>> >>     else:
>> >>
>> >>         print('unmatched')
>> >
>> >
>> > Ok, but you have to nest it inside a for loop. Your class is a good
>> > solution, but a new syntax does not need nesting in a with or a for
>> > statement.
>> >
>> 
>> I don't think he *has* to nest it. My reading of the above is that
>> it's the For-Case Paradigm [1], normally considered an anti-pattern
>> but viable for demonstrating what happens in each case.
>> 
>> ChrisA
>> 
>> [1] http://thedailywtf.com/Articles/The_FOR-CASE_paradigm.aspx
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140422/324295dd/attachment-0001.html>


More information about the Python-ideas mailing list