[Python-ideas] Match statement brainstorm

Greg Ewing greg.ewing at canterbury.ac.nz
Fri May 27 22:13:33 EDT 2016


Stephen J. Turnbull wrote:
> Suppose I want to destructure the Points determining the Lines
> determining an Intersection when I print an Intersection,

For the non-Dutch at least, the obvious way to spell
that would be

    switch obj:
       case Intersection(
          Line(Point(?x1, ?y1), Point(?x2, ?y2)),
          Line(Point(?x3, ?y3), Point(?x4, ?y4))):

However, that would mean there was something special about
expressions in the pattern not preceded by ?, i.e. things
that look like function calls are really sub-patterns. That
would make it difficult to use a real function call to
calculate a value to match against.

An alternative would be to require these kinds of
sub-patterns to be marked, e.g.

    switch obj:
       case Intersection(
          class Line(class Point(?x1, ?y1), class Point(?x2, ?y2)),
          class Line(class Point(?x3, ?y3), class Point(?x4, ?y4))):

For consistency, we should probably apply the same rule
to the main pattern as well:

    switch obj:
       case class Intersection(
          class Line(class Point(?x1, ?y1), class Point(?x2, ?y2)),
          class Line(class Point(?x3, ?y3), class Point(?x4, ?y4))):

Finally we could allow "case class" to be abbreviated to
just "class":

    switch obj:
       class Intersection(
          class Line(class Point(?x1, ?y1), class Point(?x2, ?y2)),
          class Line(class Point(?x3, ?y3), class Point(?x4, ?y4))):

Is that unacceptably verbose? I don't know. We could use
more random punctuation instead, but that would increase the
line-noise factor.

-- 
Greg


More information about the Python-ideas mailing list