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.
"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.