
On 2021-08-08 at 11:30:20 +0400, Abdulla Al Kathiri alkathiri.abdulla@gmail.com wrote:
... if we write “case” before “def” similar to “async” before “def” in async function it should be clear we are doing pattern matching. The function will be named case function.
case def fib(0): return 0
case def fib(1): return 1
case def fib(int(n)): return fib(n-1) + fib(n-2)
If you none of the parameters you pass match your case functions, an exception will be raised ...
Because the order of cases is significant, these would be difficult to work with in the REPL. If, for example, I start with your three definitions, and then decide to write a special case¹ for 2, how do I convince Python that
case def fib(2): return 1
belongs before the general case?
¹ Yeah, I know, special cases aren't special enough. In a non-toy case (pun intended), there could be any number of reasons to add a new case in the middle, or to redfine/fix a case that already exists rather than add a new case to the end.