
On Sun, 12 Jul 2020 at 10:47, Larry Hastings <larry@hastings.org> wrote:
In that case, I'd like to make a specific pitch for "don't make '_' special". (I'm going to spell it '_' as it seems to be easier to read this way; ignore the quotes.)
Overall, this sounds mostly reasonable. I'm cutting nearly everything here, because I don't have anything to add.
One consideration: if you do use '_' multiple times in a single pattern, and you do refer to its value afterwards, what value should it get? Consider that Python already permits multiple assignments in a single expression:
(x:="first", x:="middle", x:="last")
After this expression is evaluated, x has been bound to the value "last". I could live with "it keeps the rightmost". I could also live with "the result is implementation-defined". I suspect it doesn't matter much, because the point of the idiom is that people don't care about the value.
The problem for me is specifically with variables *other* than `_` - precisely because `_` has connotations of "don't care". If I see match expr: case Point(x, x): # what is x here? I would very strongly expect that to mean that the two components of Point were equal, and x was set to the common value. That's not what Python does, so this would be a fairly easy mistake to make. For what it's worth, it looks like Rust uses the same rule as the PEP - multiple occurrences of the same variable are not allowed, but _ is a wildcard that *can* be used multiple times, but isn't bound. Paul