Hacker News new | ask | show | jobs
by CDSlice 2192 days ago
Although it won't be as concise, you can use guards to emulate this feature with

    match data:
        case [x1, x2] if x1 == x2:
            ...
I agree on this being a fantastic addition to the language. I've sorely missed not having pattern matching in Python after using Rust.
2 comments

I think this is a better version. It is less ambiguous and more general. The [x,x] version isn't clear if it is using `__eq__` or `is`.
Guards feel superfluous if the x, x pattern can be trivially lowered into a guard.
More vice versa. Guards look more general (being able to express not-equal, and less-than etc), so special semantics for `x,x` only seem warranted if it's very common.
Agreed they're more general. If I have a working guard implementation I can build the x, x case atop it. Meaning, the x, x case can be sugar.