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

Lucas Malor 7vsfeu4pxg at snkmail.com
Sat Apr 19 01:08:58 CEST 2014


On 18 April 2014 16:58, Ed Kellett edk141-at-gmail.com |
python-ideas-at-python.org| <baswps8ght at sneakemail.com> wrote:

>     case foo():
>
> would have to become
>
>     case (foo(),):
>
> to work as expected when foo() returned a tuple, which would mean
> wrapping things in 1-tuples whenever you wanted to reliably match a
> case that is determined dynamically.
>

To obviate this, instead of tuples case could check membership of
CaseTuples. CaseTuple will be a class that extends tuple, identical to it,
and a case expression list with commas will yield a CaseTuple. To use other
iterables as case expression list you'll have to unpack them, or they will
be matched for equality.


On 18 April 2014 17:03, Joao S. O. Bueno jsbueno-at-python.org.br |
python-ideas-at-python.org| <0ucjz7dbjt at sneakemail.com> wrote:

> It may be just me, but I fail - in a complete manner - to see how this
> syntax can offer any
> improvement on current if/elif chains. It seems to differ from that
> only by reducing
> the explicitness, and the flexibility of the test condition that can
> be used in any
> of the subconditions
>

It's more simple to use when you can use it, as switch statement in the
other languages. And it somewhat adheres to the DRY principle: why repeat
the subject? If I'm checking what type of tarot card I have, why should I
repeat every time I'm trying to identify a tarot card?
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.


@Skip Montanaro: yes, switch statement is used in C also for code
optimization. Frankly, I think this aspect is unimportant for CPython in
the present time.


On 18 April 2014 18:51, Andrew Barnert abarnert-at-yahoo.com |
python-ideas-at-python.org| <3en9kh2cbt at sneakemail.com> wrote:

> No it isn't. First, the "for ... in" keywords are not the same as just
> "for" and a bunch of parens and semicolons.
>

My propose has elcase, that is not present in other languages.


Also, notice that if you try to read the switch statement, or your Python
> version, as English, it's nonsense.
>

Yes, the fact a case will behave differently for tuples and non-tuples will
be difficult to translate in English. I think that with CaseTuple proposal
it will be more understandable, since you have to explicitly unpack an
iterable, or use elements separated by commas.


Bash doesn't have separate "case" and "elcase" cases. After one case is
> done, the rest are skipped, just as in most other languages.
>

But it has ;& and ;;& similarly to break and continue of C, that it's
equivalent to case and  elcase of my proposal.


I don't see how skipping over any elcase but falling through to the next
> case is in any way simpler than C.


Well, because it's coherent with if-elif. See my last example in my first
message.


Well, then at least look at the limited form of pattern matching Python has
> in the for and assignment statements and parameter matching, and maybe look
> at how pattern matching is used with case statements in other languages;
> don't try to suggest language designs based on guesses.
>

Excuse me? I know list comprehensions, lambdas and argument unpacking. And
I do not think you can see what I do before I post a message. If so, you
could see me googling before writing about something that I don't know very
well or I don't remember very well. So don't guess about what I do or not
do or know and not know, thank you.
About pattern matching in the for statement, I really don't know what they
are.


.... and? Are you suggesting that if the switch expression is a string and
> the case expression a compiled regex you could automatically call match
> instead of testing for equality? If not, how is having regexp even relevant
> here? And how are recursive functions relevant?
>

I'm suggesting to use if-elif with re module, if you want to use regular
expression, and to use recursive functions if you want... recursive
functions. To be more clear, IMHO switch-case is useful if it's simple.


A generator expression is equal to anything except itself, and doesn't
> contain anything.
>

You can convert it to an iterable. Probably an overkill, but you can do it.


I don't know what you mean by "symbolic pattern" here.
>

For what I know (not too much), in Mathematica pattern matching can be used
for symbols, and symbols can be used as identifiers:
https://reference.wolfram.com/mathematica/guide/Patterns.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140419/2497c72a/attachment.html>


More information about the Python-ideas mailing list