On 24 Jun 2020, at 19:14, Guido van Rossum
wrote: (Jakub, next time please trim the original post from your quote to what's necessary.)
Apologies, unintentional, I was replying from the web interface since I wasn’t subscribed to this gorup when the thread was started and I clicked some wrong buttons.
So, now for the one thing that makes me unhappy: the rejected idea to make it an expression. In my short experience with pattern matching, mainly in Rust, roughly half (very vague estimate) of its usefulness came from it being an expression. It's even small things like
let i = match i { 9 => 10, 10 => 9, _ => i, };
and
let mut file: Box<Write> = match filename.as_ref() { "-" => Box::new(io::stdout()), _ => Box::new(File::create(filename).expect("Cannot open file for writing")), };
and it adds up. I'm not sure how to approach this with Python syntax and I'll think about this, but I feel that it'd be a huge missed opportunity to not have this.
We considered it, but it simply doesn't work, for the same reason that we haven't been able to find a suitable multi-line lambda expression. Since Python fundamentally is not an expression language, this is no great loss -- you simply write a match statement that assigns a value to the variable in each branch. Alternatively, the match could be inside a function and each block could return a value.
-- --Guido van Rossum (python.org/~guido) Pronouns: he/him (why is my pronoun here?)
That’s fair, but there’s a way in which this doesn’t have to be equivalent to multi-line lambda expressions. Granted, I should’ve clarified that I thought about it being an expression in a very limited, special way. Let’s take one example from the PEP text: match greeting: case "": print("Hello!") case name: print(f"Hi {name}!”) Let’s say we allow prepending “match …” with an assignment and the value of the assignment is the value of the last statement/expression in the block that’s selected, this allows for the following hypothetical code: message = match greeting: case "": "Hello!" case name: f"Hi {name}!” So I didn’t express this clearly – it’s not a bout full-blown match expressions but rather an optional "assigning form” of match statements. This seems like it wouldn’t affect parsing massively. Jakub