[Python-Dev] switch-based programming in Python

Greg Ewing greg@cosc.canterbury.ac.nz
Fri, 09 Nov 2001 15:36:59 +1300 (NZDT)


Thomas Wouters <thomas@xs4all.net>:

>  switch EXPR:
>  case CONSTANT:
>      [suite]
>  case CONSTANT:
>      [suite]
>  ...
>  else:

Looks good, except that I'd indent the cases as well, i.e.

  switch EXPR:
    case CONSTANT:
      [suite]
    case CONSTANT:
      [suite]
    else:
      [suite]

To my mind the cases are logically a subordinate part of the 
switch statement, and the indentation should reflect that.

Some alternatives:

Using only one keyword:

  case EXPR:
    CONSTANT:
      [suite]
    CONSTANT:
      [suite]
    else:
      [suite]

Using two, but reminding one less of C:

  case EXPR:
    of CONSTANT:
      [suite]
    of CONSTANT:
      [suite]
    else:
      [suite]

Possible refinements:

* Multiple values in a case

  CONSTANT, CONSTANT, ..., CONSTANT:

* Ranges in a case

  CONSTANT..CONSTANT:

although that would require something other than a dict,
maybe a binary search. Also could lead to arguments about
whether the endpoint should be inclusive or exclusive!
Maybe it should be spelt

  range(CONSTANT, CONSTANT):

?

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg@cosc.canterbury.ac.nz	   +--------------------------------------+