Re: PEP 622: Structural Pattern Matching

Eric Nieuwland <eric.nieuwland@gmail.com>:
I have some doubt about the keyword Guido van Rossum [mailto:guido@python.org]: Many people have tried to come up with a different keyword here, but nothing has been found that comes even close to the simplicity of match. Plus, several other languages (Scala, Rust) use it too (which is further evidence that it's a natural fit).
I was thinking that 'match' and 'case' are reversed: The top line presents the _case_ to be studied. The patterns that follow try to _match_ the presented case. Trying it out on PEP example: case input: match x if x > MAX_INT: print("Got a large number") I think it reads well, in particular when like here there's a condition attached. The second line is almost English: "match x if x greater than max int". Pascal is a precedent for this placement of 'case', although of course Niklaus Wirth is not a native English speaker either. regards, Anders

On 25/06/20 7:50 pm, Anders Munch wrote:
Pascal is a precedent for this placement of 'case',
Yes, but it doesn't use it with "match". In fact it doesn't have any keyword in front of the values to be matched; it goes like case n of 1: ...; 2: ...; 3: ...; end If we did that in Python it would look like this: case shape: Point(x, y): ... Line(x1, y1, x2, y2): ... Circle(cx, cy, r): ... What think folks of this? -- Greg

On 2020-06-25 03:00, Greg Ewing wrote:
On 25/06/20 7:50 pm, Anders Munch wrote:
Pascal is a precedent for this placement of 'case',
Yes, but it doesn't use it with "match". In fact it doesn't have any keyword in front of the values to be matched; it goes like
case n of 1: ...; 2: ...; 3: ...; end
If we did that in Python it would look like this:
case shape: Point(x, y): ... Line(x1, y1, x2, y2): ... Circle(cx, cy, r): ...
What think folks of this?
IMHO it looks a lot like shell scripting, which isn't really a bad thing in general, but the second form seems to lose some readability to me -- Joseph

On 06/25/2020 03:00 AM, Greg Ewing wrote:
On 25/06/20 7:50 pm, Anders Munch wrote:
Pascal is a precedent for this placement of 'case',
Yes, but it doesn't use it with "match". In fact it doesn't have any keyword in front of the values to be matched; it goes like
case n of 1: ...; 2: ...; 3: ...; end
If we did that in Python it would look like this:
case shape: Point(x, y): ... Line(x1, y1, x2, y2): ... Circle(cx, cy, r): ...
I strongly prefer "match expression: case ...:" -- ~Ethan~

On Thu, Jun 25, 2020 at 3:17 AM Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
On 25/06/20 7:50 pm, Anders Munch wrote:
Pascal is a precedent for this placement of 'case',
Yes, but it doesn't use it with "match". In fact it doesn't have any keyword in front of the values to be matched; it goes like
case n of 1: ...; 2: ...; 3: ...; end
If we did that in Python it would look like this:
case shape: Point(x, y): ... Line(x1, y1, x2, y2): ... Circle(cx, cy, r): ...
What think folks of this?
It feels like it would be the only bit of syntax in Python that requires a `:` and yet has no keyword to go with it for clarification of what the block is meant for. That makes me not like this.

On 2020-06-25 08:50, Anders Munch wrote:
Eric Nieuwland <eric.nieuwland@gmail.com>:
I have some doubt about the keyword Guido van Rossum [mailto:guido@python.org]: Many people have tried to come up with a different keyword here, but nothing has been found that comes even close to the simplicity of match. Plus, several other languages (Scala, Rust) use it too (which is further evidence that it's a natural fit).
I was thinking that 'match' and 'case' are reversed: The top line presents the _case_ to be studied. The patterns that follow try to _match_ the presented case.
Trying it out on PEP example:
case input: match x if x > MAX_INT: print("Got a large number")
I think it reads well, in particular when like here there's a condition attached. The second line is almost English: "match x if x greater than max int".
Pascal is a precedent for this placement of 'case', although of course Niklaus Wirth is not a native English speaker either.
Some languages use 'case' at the start of the construct; other languages use 'case' for the individual values. There are precedents for both placements.
participants (6)
-
Anders Munch
-
Brett Cannon
-
Ethan Furman
-
Greg Ewing
-
Joseph Jenne
-
MRAB