News for hackers. Stuff that matters.
Re:Real-World Match/Case by UncleOxidant on Wednesday December 13, 2023 @06:13PM (#38635569)
That's really nice. One of the things I really like about languages like OCaml is the pattern matching. I had no idea that something like this was added to Python 3.10.
Re:Real-World Match/Case by richard_shelton on Wednesday December 13, 2023 @06:40PM (#38635816)
Here are more real-world, compiler-related examples: https://github.com/true-grue/python-dsls/blob/main/python_ds...
Re:Real-World Match/Case by tialaramex on Wednesday December 13, 2023 @07:34PM (#38636276)
There's surely some sort of ambition within Python to catch up on all the mistakes C++ made, despite its head start. Here, for example, quoting Python's documentation:

"Name bindings made during a successful pattern match outlive the executed block ... During failed pattern matches, some subpatterns may succeed. Do not rely on bindings being made for a failed match. Conversely, do not rely on variables remaining unchanged after a failed match. The exact behavior is dependent on implementation"

Python also - perhaps as expected given its nature as a dynamically typed language - doesn't end up delivering irrefutable matching that you might be familiar with from functional languages with a pattern match. So you're going to need to write that default case just on the chance you screwed up and missed something, if you forgot to handle Tuesday this design just assumes you intended to ignore Tuesdays because you didn't mention them.

Re:Real-World Match/Case by hoosieree on Wednesday December 13, 2023 @08:01PM (#38636484)
Amazing! Python is consistently bringing some of the best-loved features from other languages to the mainstream, and then ever-so-subtly ruining them.
Re:Real-World Match/Case by a_humean on Wednesday December 13, 2023 @08:41PM (#38636780)
My main problem with this implementation in python is that they are statements and not expressions. They would be a lot more natural to use if they were expressions, but python is a very statement oriented language. That is a thing I dislike about python.

From what I can tell adoption of them is very slow going in the python world despite it being a pretty good implementation of structural pattern matching. Pyright/pylance is also able to reason well around them.