mathieu
2021-2-11 15:23:54

@mathieu has joined the channel


samdphillips
2021-2-11 18:45:09

Python is adding Pattern Matching: https://www.python.org/dev/peps/pep-0634/


samdphillips
2021-2-11 18:47:09

Looking at the proposal the big thing that is standing out to me is how syntactically this increases right-ward drift.


popa.bogdanp
2021-2-11 19:37:37

The behaviour of <https://www.python.org/dev/peps/pep–0634/#capture-patterns|Capture Patterns> is an odd choice for a language with Python’s scoping rules.

a = 42 match 43: case a: pass print(a) would print 43 IIUC, which is pretty unfortunate (though, I guess, consistent with other stuff in the language)


samdphillips
2021-2-11 19:39:06

Yes I thought that was an bad choice


samdphillips
2021-2-11 19:39:45

The language about irrefutable patterns seemed a bit over specified too


samdphillips
2021-2-11 19:41:14

Actually I think that example would print 42 or 43 depending on the implementation


popa.bogdanp
2021-2-11 19:42:32

How do you mean?


samdphillips
2021-2-11 19:44:37

Oh wait you’re correct. Further up they talk about binding and failed matches: > During failed pattern matches, some subpatterns may succeed. For example, while matching the pattern (0, x, 1) with the value [0, 1, 2], the subpattern x may succeed if the list elements are matched from left to right. The implementation may choose to either make persistent bindings for those partial matches or not. User code including a match statement should not rely on the bindings being made for a failed match, but also shouldn’t assume that variables are unchanged by a failed match. This part of the behavior is left intentionally unspecified so different implementations can add optimizations, and to prevent introducing semantic restrictions that could limit the extensibility of this feature.


popa.bogdanp
2021-2-11 19:45:54

I guess this must be why they specify irrefutable patterns like they do


popa.bogdanp
2021-2-11 19:47:40

Oh, nope, I misunderstood the part about irrefutable case blocks! I was thinking of cases like

case a: ... case b: ... but I suppose then the a case will always assign and you may or may not have b binding at the end (depending on if it already existed)


popa.bogdanp
2021-2-11 19:49:35

Third re-read’s the charm :sweat_smile:, I did have it right. The above example would be a syntax (or something) error, I think.


samdphillips
2021-2-11 19:49:46

I was thinking about cases like: a = 100 b = 200 match [0, 1]: case [a, 2]: pass case [0, b, 3]: pass case _: pass print(a) print(b)


popa.bogdanp
2021-2-11 19:50:39

ah, yeah, that would be no fun to debug at all


samdphillips
2021-2-11 19:50:42

(that’s presuming the second case doesn’t do a length check before trying the match)


samdphillips
2021-2-11 19:54:34

Oh and you could probably do some horrible things in guards to make the binding more confusing


samdphillips
2021-2-11 20:05:36

I was actually surprised they didn’t add more magic_methods and then have more extensible matching


samdphillips
2021-2-11 20:16:09

More about Python match in case anyone is interested. A friend recommended the LWN coverage. https://news.ycombinator.com/item?id=26080760


plragde
2021-2-11 21:12:21

Now do scope


samdphillips
2021-2-11 21:29:32

I think someone posted the 2013 Python paper the other day :smile:.

the tldr about match and scope in python is that it is still bad.


jesse697
2021-2-12 05:58:29

FWIW PHP 8.0 (the newest major release, just a few months old) added a match to the language: https://www.php.net/releases/8.0/en.php#match-expressionhttps://www.php.net/releases/8.0/en.php#match-expression