korede
06/25/2020, 9:15 PMDan Cook
06/25/2020, 9:21 PMChris Maughan
06/25/2020, 9:23 PMChris Maughan
06/25/2020, 9:24 PMshalabh
06/25/2020, 10:16 PMshalabh
06/25/2020, 10:17 PMshalabh
06/25/2020, 10:21 PMIonuț G. Stan
06/26/2020, 6:13 AMshalabh
06/26/2020, 6:59 AMcase
is surprising if you know Python. It's not regular Python, it looks regular but is designed to work as a structure matching (and name binding language). Consider:
case 123:
- matches the value 123. so far so good
abc = 123
case abc:
- matches 123? No, it matches any value because abc
is a name pattern (will capture any value). What if you really wanted to match the value in the variable abc
? Well now there's a brand new syntax (which only works inside case): case .abc
.
Then there's the class patterns, which look like calls but actually capture values from an existing structure:
case Point(x, y)
- creates/sets x and y, if the object is a Point.
Anyway the PEP is still being worked on so lets see what it looks like eventually.Ionuț G. Stan
06/26/2020, 7:34 AMEdward de Jong / Beads Project
06/26/2020, 7:42 AMIonuț G. Stan
06/26/2020, 8:05 AMshalabh
06/26/2020, 8:18 AMexcept E as e
syntax - it's already kind of a pattern match using just isinstance. Would we not want matching to work identically in that syntax as well? IIRC you can have destructuring assignment in Erlang and put constants on the left, roughly "red", a = f()
- this only matches and binds iff f() returns "red" as the first item in a pair. There's no such thing in Python, but you can have only free variables on the left that always get bound. Should that also work now, given the unpacking pattern can use case ["red", a]
?Konrad Hinsen
06/26/2020, 12:30 PMPoint(x, y)
not only looks like a constructor but isn't, it also interprets arguments in a different way from a constructor. Sure, one would expect Python programmers to ensure that a class' __init__
and __match__
are semantically compatible, but I bet we will see many classes whose __init__
allows more variations than __match__
. And I also bet that people will try to use class(**args)
as a pattern and expect class
to be bound to the class object.shalabh
06/26/2020, 5:25 PMcase Regex('a.*b')
- the inner pattern is not sent to the class, but the class is supposed to return a 'static data field like' structure. So this also will cause problems if you have a few expensive computed properties - are you know supposed to generate and return all of them? What if the caller is only matching one?Konrad Hinsen
06/26/2020, 6:25 PMIonuț G. Stan
06/26/2020, 7:50 PMpattern = Regex('a.*b')
match 'aaaab':
case pattern(matched, capturingGroup1, etc): pass
Regarding your other point, about expensive computations, the PEP says this:
There is no requirement that the attributes on the proxy object be the same type or value as the attributes of the original object; one envisioned use case is for expensive-to-compute properties to be computed lazily on the proxy object via property getters.Also, your concerns about pattern matching for exceptions and assignments are well founded, but it seems to me that they can add these capabilities later.
Ionuț G. Stan
06/26/2020, 7:56 PMreturn
or yield
and write our programs in a completely continuation-passing style, which would be quite hard to understand.
I feel that once you'll be able to play with it, you'll like it more, unless you've already used pattern matching in some other language and decided it was a bad idea there.shalabh
06/26/2020, 9:01 PM__match__
should be a class or static method, so we'll be creating one class per regex.shalabh
06/26/2020, 9:03 PMa = f()
is clear and a.b = f()
kinda follows. But case a
is totally different from case a.b
- in the latter case it's a reference, the the former case a
is a name to be bound.shalabh
06/26/2020, 9:09 PMcase Point as x,y
or case Point(?x, ?y)
Ionuț G. Stan
06/26/2020, 9:11 PMcase .a.b
would work instead of case a.b
. And I get your point, but on the other hand, the main purpose of pattern matching is to introduce new bindings, so the main use case should introduce as little syntax as possible, I think.Ionuț G. Stan
06/26/2020, 9:12 PMshalabh
06/26/2020, 9:58 PMx
and x()
are valid, the x
part means the same exact thing. But here one is a new name and one is a reference. What if I wanted to match a specific point at a known x,y? case Point(x, y)
wont work, but I feel it should work, given I can do case "hello"
and have the value match. Literals and predefined constants get special features here.korede
06/27/2020, 6:36 PM