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


Groups > comp.lang.python > #69673

Re: Yet Another Switch-Case Syntax Proposal

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: Yet Another Switch-Case Syntax Proposal
Date 2014-04-04 19:44 +0300
Organization A noiseless patient Spider
Message-ID <87ppkx6o4p.fsf@elektro.pacujo.net> (permalink)
References <8084-1396540962-768613@sneakemail.com> <mailman.8852.1396550862.18130.python-list@python.org> <533e6e58$0$29993$c3e8da3$5496439d@news.astraweb.com> <87ioqpqvef.fsf@elektro.pacujo.net> <mailman.8894.1396627349.18130.python-list@python.org>

Show all headers | View raw


Ian Kelly <ian.g.kelly@gmail.com>:

> On Apr 4, 2014 3:51 AM, "Marko Rauhamaa" <marko@pacujo.net> wrote:
>>    switch: local_sabbath()
>>    case (1, 2, 3) as sabbath:
>>        ...
>>    case 6:
>>        ...
>>    else:
>>        ...
> [...]
>
> What's wrong with the much more natural "switch local_sabbath():"?

Consider:

    switch local_sabbath():        # bad
    case (1, 2, 3) as sabbath:
        ...

Now Python "framing" requires that you place something between the first
":" and "case":

    switch local_sabbath():        # bad
        pass
    case (1, 2, 3) as sabbath:
        ...
    
Placing the expression after the colon terminates the first colon
cleanly. Also, the "lambda" precedent allows an expression to follow a
colon; both "lambda" and my "switch" mandate that the expression stay on
the same line with the colon.

> Second, "as" clauses are used in other contexts for local assignment.
> What is the purpose of doing that here? How does this solve the
> problem of explicitly denoting case multiplicity?

The "as" clause follows the precedent of the "try/except" statement. It
removes the occasional annoyance in C:

   switch (next_char()) {
   case '\n':
   case '\r':
       putchar(???);
        :   :   :

which forces you to introduce a temporary variable:

   char c;
        :   :   :
   c = next_char();
   switch (c) {
   case '\n':
   case '\r':
       putchar(c);
        :   :   :

It is most useful in the "default"/"else" branch:

   switch: q.pop()
   case 0:
       log_direction(0)
       return 1
   case (90, 270) as angle:
       log_direction(angle)
       return 0
   case 180:
       log_direction(180)
       return -1
   else angle:
       log_direction(angle)
       return math.cos(angle * 2 * PI / 360)


Marko

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


Thread

Re: Yet Another Switch-Case Syntax Proposal Ethan Furman <ethan@stoneleaf.us> - 2014-04-03 11:23 -0700
  Re: Yet Another Switch-Case Syntax Proposal Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-04 08:33 +0000
    Re: Yet Another Switch-Case Syntax Proposal Marko Rauhamaa <marko@pacujo.net> - 2014-04-04 12:46 +0300
      Re: Yet Another Switch-Case Syntax Proposal Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-04 10:02 -0600
        Re: Yet Another Switch-Case Syntax Proposal Marko Rauhamaa <marko@pacujo.net> - 2014-04-04 19:44 +0300
          Re: Yet Another Switch-Case Syntax Proposal Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-04 11:55 -0600

csiph-web