I understand the worry that newcomers might struggle, but I don't think it's going to be the case: newcomers regularly learn the languages listed above without stumbling across that problem. And if Python did choose a syntax like the one you're proposing, it'd also be the odd one out among dozens of mainstream languages including this feature, which I think would be even more confusing!
I can't wait for this feature!
The entire point of structural pattern matching is that structuring and destructuring look the same.
> This syntax goes against Zen: It’s implicit -- when using match case expressions don't mean what they regularly mean.
There's nothing implicit to it. The match/case tells you that you're in a pattern-matching context.
> I’m a big believer in this feature, it just needs some other syntax. Using {} instead of () makes it a lot better. Now no way to confuse it with simple equality.
Makes it even better by… looking like set literals and losing the clear relationship between construction and deconstruction?
I used to feel like I could define __getattr__ or __setattr__ and understand the implications, but that's getting increasingly terrifying.
Behavior doesn't change at all.
Python is good enough without them that they never had to learn them.
In fact, today I'm teaching "advanced python" to someone that has been coding in python for a year.
This person didn't know about list comprehensions.
I would not worry about that.
It doesn't bother me since I see that match/case is a whole different context, I know I'm matching, and have no reason to instanciate anything here.
But since Foo(a=1) looks like instanciation, it's obvious to me what it's supposed to match.
I think that's one of the things that motivated to:
- use new keywords, but existing ones like "as", nor symbols;
- force 2 levels of indentation despite the cost of it.
This makes it very clear, with little effort, that the context is completly different.
It will make scanning the code easy, and avoid confusion.
Using a different syntax for pattern matching loses the main point of pattern matching.
match shape:
case Point(x, y):
...
case Rectangle(x, y, _, _):
...
print(x, y) # This works match node:
on (time _ 1 @ 0, foo _ 2, bar _ 3, baz _ any, status _ 1 @ -1):
do_something()
rematch do_something_2(foo)
'_' indicating span, and '@' indicating position of the first item. Both can be omitted.
Seems like it doesn’t create instances when you’re doing
instead:1. Node means "is instance of Node".
2. Everything in between () is "has an attribute with value".
3. List means "the attribute should be treated as a tuple of".. etc..
Very confusing, this definitely needs another syntax, because both newcomers and experienced devs will be prone to read it as plain `==`, since that's how enums and primitives will be working.
This syntax goes against Zen: It’s implicit -- when using match case expressions don't mean what they regularly mean. It’s complicated -- basically it’s another language (like regex) which is injected into Python.
I’m a big believer in this feature, it just needs some other syntax. Using {} instead of () makes it a lot better. Now no way to confuse it with simple equality.