
@mathieu has joined the channel

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

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

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)

Yes I thought that was an bad choice

The language about irrefutable patterns seemed a bit over specified too

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

How do you mean?

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.

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

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)

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.

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)

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

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

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

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

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

Now do scope

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.

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