Assignment statements and parameters should allow patterns
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Mars |
Fix Committed
|
Low
|
Matt Giuca |
Bug Description
Currently, assignments are made to variable names, which means the expression on the right's value is always assigned to a given variable.
The language would be more expressive, and in some ways, simpler, if assignments could be made to a pattern, instead of a variable name. This would give assignment statements non-primitive semantics, as they could be expressed in terms of a switch statement, as follows:
p = e
is syntactic sugar for:
switch e:
case p:
pass
This would mean that assignments could be made to underscores (making them useless, underscores are recursively useful so there is no reason to ban them at the top level -- and Python allows it too), or more usefully, to ctor patterns, allowing unpacking to be done in an assignment statement. Note that the same exhaustion restrictions as switch statements would apply, meaning you wouldn't be able to use an int pattern, and you could only use a ctor pattern for a type with a single alternative (otherwise, use a switch).
In the documentation, explain it as in the previous paragraph first (and explain the syntactic sugar in a note).
Changed in mars: | |
milestone: | none → 0.4 |
Do the same for function arguments, for uniformity. This means you can unpack patterns in function arguments, but does not let you switch over a multi-alternative ADT.
This would supersede bug #439164, allowing the use of "_" in place of a function argument.