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

> On Apr 4, 2014 3:51 AM, "Marko Rauhamaa" <ma...@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
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to