On 2020-06-23 18:20, Antoine Pitrou wrote:
Some comments:
* What you call "Constant Value Patterns" can really refer to any local or non-local name, regardless of how complex the referred object is, right? Also, __eq__ is called in that case, not __match__?
* If I understand correctly, these:
case b"": print("it's an empty bytes object")
and
case bytes(): print("it's a bytes object")
have entirely different meanings. Am I right? This sounds like I have to switch contexts when reading code, based on whether I am reading regular code or a match clause, given that semantics are quite different.
Instead, it seems like the latter would be more explicitly spelled out as, e.g.:
case instanceof(bytes): print("it's a bytes object")
* The acronym "ADT" is never defined.
* """If there are more positional items than the length of __match_args__, an ImpossibleMatchError is raised."""
What if there are less positional items than ``len(__match_args__)``? Can the match succeed rather than raise ImpossibleMatchError? This seems potentially error-prone.
Overall, my main concern with this PEP is that the matching semantics and pragmatics are different from everything else in the language. When reading and understanding a match clause, there's a cognitive overhead because suddently `Point(x, 0)` means something entirely different (it doesn't call Point.__new__, it doesn't lookup `x` in the locals or globals...). Obviously, there are cases where this is worthwhile, but still.
It may be useful to think about different notations for these new things, rather than re-use the object construction notation.
For example:
case Point with (x, y): print(f"Got a point with x={x}, y={y}")
or:
case Point @ (x, y): print(f"Got a point with x={x}, y={y}")
(yes, "@" is the matrix multiplication operator... but it's probably much less likely to appear in common code and especially with a class object at the left)
Or: case Point as (x, y): print(f"Got a point with x={x}, y={y}") perhaps as 'as' is already used for binding. The disadvantage there is with nested patterns: case Coordinate(Point(x1, y1), Point(x2, y2)): where you're matching a coordinate and binding to x1, y1, x2 and y2.