[Python-ideas] Pattern Matching Syntax
rosuav at gmail.com
Thu May 3 13:13:21 EDT 2018
On Fri, May 4, 2018 at 12:29 AM, Robert Roskam <raiderrobert at gmail.com> wrote:
> Hey Joao,
> Thanks for providing me feedback on this idea!
> For the simplistic example at that you select, yes, you absolutely can do
> this as it stands atm. However, the examples I provided further along aren't
> as easily accomplished, nor is something like this:
> x = -1
> result = match x:
> x:int if x > 0 => 'greater than 0'
> x:int, x:float if x == 0 => 'equal to 0'
> x:int if x < 0 => 'less than 0'
> print(result) # 'less than 0'
> Accomplishing the above with a just a dictionary would be not be the current
> Pythonic solution, imo, you'd do it with if/elif:
Correct. So the best way to 'sell' this idea is NOT the simple
examples, as they're going to be just as simple with a dictionary.
I'd like to see a complete definition of the valid comparisons. Some
of your examples are based on equality ("5 => ..." matches the number
5, presumably whether it's an int or a float) and have no variable,
others use annotation-like syntax and types to presumably do an
isinstance check, and then there's some conditions that I'm not sure
about. What are all the options and how would each one be written? How
do you combine different options? Is the 'if x == 0' part a modifier
to a previous comparison, or is it a separate chained comparison? How
does this work?
More information about the Python-ideas