29 ms·
From what I gather from that article is that pattern matching is the new buzzword for Switch Cases? If so, expect an influx of applications misusing switches i
by doublerabbit 6y ago
From what I gather from that article is that pattern matching is the new buzzword for Switch Cases?
If so, expect an influx of applications misusing switches in replacement of if.
- cassianoleal 6y agoIt's certainly not, although it can be used in switch cases. Pattern matching is a great language feature but I've been watching the development of it for Python for a while and have felt nothing but horror. If you want a great example of how it can be useful and how clear and elegant it can be, check out how it's done in Elixir [0]. There are many other languages that implement it in great ways, I'm sure others will comment on them. [0] https://elixir-lang.org/getting-started/pattern-matching.html https://elixir-lang.org/getting-started/pattern-matching.htm...
- sammorrowdrums 6y agoYes,oddly I love it I Rust, it is probably a favourite feature, but I am not really excited about it in Python. https://doc.rust-lang.org/book/ch06-02-match.html https://doc.rust-lang.org/book/ch06-02-match.html
- aynyc 6y agoLooking at the examples in the doc. That’s just tuple/list unpacking in Python. Am I misreading it?
- ketzu 6y agoThe 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 ^. [1] https://www.reddit.com/r/programming/comments/lgqhmj/stack_overflow_users_rejoice_as_pattern_matching/gmtypn2/ https://www.reddit.com/r/programming/comments/lgqhmj/stack_o...
- fortran77 6y ago> If you want a great example of how it can be useful and how clear and elegant it can be, check out how it's done in Elixir [0]. If you want to see what it _really_ should look like, see Erlang, and then look at Prolog.
- jml7c5 6y agoMuch more than a switch statement. Here's PEP 636, a tutorial: https://www.python.org/dev/peps/pep-0636/ https://www.python.org/dev/peps/pep-0636/
- tirpen 6y agoYes, 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.
- willxinc 6y agoPattern 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.