The problem that arises here is one of pythons scoping and people reading this satire will have a hard time understanding what is happening (if they don't follow the links in the article).
Taking an example from a reddit comment[1] for the actual pattern matching. The command matched on is a list, and the matching condition in this case is the number of elements in the list.
command = ['cmd', 'somearg']
match command:
case [nonargaction]:
print(f'We got a non-argument command: {nonargaction}')
case [action, arg]:
print(f'We got a command with an arg: {action}, {arg}')
case _:
print('default, dunno what to do')
The case gives you named arguments representing the matched strcutre. The scoping problem is, that python will overwrite an existing variable at this scope.
Seen here with loops:
x = 0
for x in range(5):
pass
print(x) # = 4
m = 5
match m:
case x: pass
print(x) # = 5
Other examples and applications can be found in one of the relevant PEPs 635 [2] (634 is the specification I believe).
case woosh:
print(“We were starting to worry that we would run out of major language changes to confuse new developers. But thanks to pattern matching, we should be set for another 5-7 years! ”)
While I initially thought this was absurd, looking at the intended use case I think this is actually sensible behavior. While I would probably use a word other than case to further distinguish it from the switch statements of other languages, the fact remains it's not a switch statement, and assigning values to variables is the whole point.
In the article's example, copied below, the programmer is confident that the status code fits the format they are looking for but not what status code corresponds to not found
NOT_FOUND = 404
match status_code:
case 200:
print("OK!")
case NOT_FOUND:
print("HTTP Not Found")
This should be handled by an elif statment. If on the other hand we're dealing with
match ambiguous_response:
case status_code:
print("Responded with status code: ",status_code)
case { "status_code" : status_code , _ }:
print("Responded with json obj containing status code: ",status_code)
doing so with elif statements would be cumbersome at best.
People seem to read the words "Structural pattern matching" and interpret it as "Pattern matching"...
It's a neat little misnamed feature, that's all...
The linked doc seems to show only what python does as tuple unpacking, but as far as I know elixir can also do the matched unpacking. Based on [1] (which also gives a more matchy example), elixir just has a warning when not using the variable in the case block and you can denote that you want to match against the contents of the variable by prepending ^.
Yes, just like functions are just a new buzzword for goto.
Structural pattern matching is a lot more than just switch/case, and I while I think pythons implementation looks imperfect, it will still be an amazing boon to the language.
There are a few gotcha-edge cases, but they are of the kind that are obvious once you've encountered them once.
I already know a couple dozen functions in my projects that I'm going to refactor to a fraction of their current length and improve the readability of at the same time as soon as this lands.
Pattern matching is distinct from switch cases, this is where the "odd" behavior people are taking issue with stems from. If it were a switch case statement, the case behavior would behave differently.
Crikey, that's going to be confusing. Why not separate capture variables from comparison variables by using tuple-style brackets?
RED = "red"
colour = "green"
match colour:
case (RED,):
print(f"The colour is {RED}")
> The colour is green
match colour:
case RED:
print(f"The colour is {RED}")
> The colour is red
Taking an example from a reddit comment[1] for the actual pattern matching. The command matched on is a list, and the matching condition in this case is the number of elements in the list.
The case gives you named arguments representing the matched strcutre. The scoping problem is, that python will overwrite an existing variable at this scope. Seen here with loops: Other examples and applications can be found in one of the relevant PEPs 635 [2] (634 is the specification I believe).[1] https://www.reddit.com/r/programming/comments/lgqhmj/stack_o...
[2] https://www.python.org/dev/peps/pep-0635/