by pansa2 · 5y ago · 0 comments
So how do I do a bitwise-or operation within a pattern?
I think given issues like this and the commonly-cited NOT_FOUND example [0], it might have been better to only allow pattern matches on compound data and disallow its use with simple values. That’s where pattern matching is most useful, as in the article‘s second example (`match point`), plus it would make it more obvious that the behaviour of `match` is different from C’s `switch`.
[0] The following code unintuitively assigns the value of status_code to the variable NOT_FOUND:
NOT_FOUND = 404
match status_code:
case NOT_FOUND:
print("HTTP Not Found")
Edit: OK, it looks like you can’t do arithmetic/bitwise operations in a pattern at all, so there’s no direct clash between bitwise or and this use of `|`.Therefore if you need to match against a calculated value, you can’t do the calculation inline. Instead you need to calculate the value outside the match and give it a name - but you can’t match against a named local variable either!
I fear a poor design like this is going to not only discourage people from using the feature at all, it is also going to poison the well and discourage people from using pattern matching in languages with a much more robust implementation. Which is a shame, because I actually like pattern matching.
(Tinfoil-hat take: Maybe that’s the whole point, after all Guido doesn’t like functional programming.)
Meanwhile, the top post on /r/python for the last few days has been "Ladies and gentlemen - switch cases are coming!" This feature and its footgun syntax are going to be a disaster for the casual Python audience.
« A match statement may have at most one irrefutable case block, and it must be last. »
So if I understand correctly the following will be an import-time error.
NOT_FOUND = 404
I_AM_A_TEAPOT = 418
match status_code:
case NOT_FOUND:
print("HTTP Not Found")
case I_AM_A_TEAPOT:
print("HTTP I'm a teapot")I frequently miss immutable data when working in Python; in general I’m conflicted about languages that retrofit functional programming features, because immutability is such a huge benefit.
I do wish we had better first-class immutable data in Python, but that wouldn't help with the
NOT_FOUND = 404
match val:
case NOT_FOUND:
...
scenario that everyone is (rightly) fixated on, because integers themselves are already immutable.Your "rightly" obviously varies, but this scenario is the poster child for enums, and you can match on enum members. The featured article has an example.
It is not that confusing once you have grooked it.
In ML and Rust, mistyping a a data constructor's name produces a variable, and thus matches everything, whereas OCaml and Haskell require that constructors start with an uppercase name, and disallows variables to do so, so this cannot easily occur.
But, since in Rust data constructors are typically qualified with a namespace except for some frequently used ones, this is not a problem in practice.
Neat syntax overall but it's not clear what it makes more succinct than the equivalent `elif` statements.
> it's not clear what it makes more succinct than the equivalent `elif` statements
But pattern matching is shorter than if-else in many cases and can actually differentiate between structures which makes it very flexible. It's like switch-case should have been from the start imho.
As an example, in Rust I often do comparisons between certain values by simply matching a tuple of them:
match (foo, bar) {
(x, x) => true, // foo == bar
(Foo::VariantA(42), _) => unreachable!(), // has to be a bug
(Foo::VariantA(a), _) => a < 0,
_ => false,
}
This is of course a rather simple example, but I hope it still
conveys what I like so much about this kind of feature. It
doesn't do more than an if-else chain, but under the right
circumstances it really shines. Just like list comprehensions
don't add any new functionality, but the filtering and mapping
features are often simply more expressive for what you want to
do.Although I'm not sure what to think of Python's preference to put the condition and code in different lines; maybe that's optional, which could make it more succinct when the situation permits it.
This seems sensible and intuitive, `NOT_FOUND` is not different than `not_found` or `bob`. It follows the normal rules of Python bindings.
I'm way more annoyed by the special case for dotted names.
[0] https://twitter.com/brandon_rhodes/status/136022610839909990...
And hey, if it finally gets explicit variable declarations in Python I'm 100% all for it. implicit locals, `global`, and `nonlocal` are awful, viva `let` and `const` or whatever.
As to the content of the tweet
> The Substitution Principle, learned in algebra?
Python is not algebra, and already breaks algebraic reasoning to and through. Just consider that `a = a + b` and `a += b` are completely different operations with possibly (and actually, for some types) completely different effects.
Getting off-topic, but IMO that’s a major design flaw in the language. Some others agree, e.g. https://twitter.com/joepolitz/status/1304992696293978112
Can you do either of those in other popular languages with pattern matching? Presumably the reason for wanting pattern matching wasn't to invent something completely new, but to implement something similar to pattern matching in other languages. (I know that C-style switches allow you to write constant arithmetic expressions in case labels.)
Ruby supports matching against a local variable by pinning the variable (using a ^ prefix).
LAST_MATCHED_STATUS_CODE = 404
match status_code:
case LAST_MATCHED_STATUS_CODE:
print("HTTP Not Found")I gather that the proposed solution is to use something like enums? Or not do this at all?
import foo
not from foo import BAR, BAZ, QUX