Assignment statements and parameters should allow patterns

Bug #513633 reported by Matt Giuca
6
This bug affects 1 person
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).

Tags: feature parser

Related branches

Revision history for this message
Matt Giuca (mgiuca) wrote :

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.

Revision history for this message
Matt Giuca (mgiuca) wrote :

De-milestoning; too major a change.

summary: - Assignment statements should allow patterns
+ Assignment statements and parameters should allow patterns
Changed in mars:
milestone: 0.3 → none
Revision history for this message
Matt Giuca (mgiuca) wrote :

See bug #595837 for a discussion on the parsing aspects of this fix.

tags: added: parser
Matt Giuca (mgiuca)
Changed in mars:
milestone: none → 0.4
Revision history for this message
Matt Giuca (mgiuca) wrote :

Fixed in trunk r1298.

Changed in mars:
status: Triaged → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.