Multiple Assignment

MultipleAssignment is a language property of being able to assign one value to more than one variables. It usually looks like the following:

 a = b = c = d = 0 // assigns all variables to 0.
This usually means/is SyntacticSugar for:

 d = 0; 
 c = d; 
 b = c; 
 a = b
This is important in languages with PassByReference or WeakTyping (ImplicitTyping?)--in the former case, a, b, c, and d all refer to the same object, and in the latter case, if, say, the value is the string "10 ducks" or the float 10.9, and d is an int, the other variables will all contain the value 10, even if they were strings or floats.

Sometimes confused with AssignmentsAreExpressions, it is not. PythonLanguage has MultipleAssignment, but not AssignmentsAreExpressions. However, if a language has AssignmentsAreExpressions, it is impossible to keep it from having MultipleAssignment.


Are you sure you're not using the term WeakTyping when you mean DynamicTyping?

No, I am NOT. What I am saying is that, if it is not an error to assign, say, "0123 ducks" to an int var, as, say, you can in Perl, the " ducks" is lost and the string is parsed to be "0123", or the int 123. This would mean that then, a string var assigned to the value of the int var would hold the string "123". Python has DynamicTyping, but it does NOT do that, assigning a string value to an int var makes the int var into a string var, it's DIFFERENT.

Whilst WeakTyping and DynamicTyping do lack rigorous definitions and are somewhat subject to interpretation, what you describe is generally considered to be DynamicTyping, as the semantics are clearly predictable and well-defined. WeakTyping generally refers to those languages where one type can be cast into another with a system-dependent, and otherwise undefined effect. For example, the following code demonstrates that C is WeaklyTyped, because an integer can be cast as a pointer, but the resulting behaviour is entirely system-dependent:

            int my_integer = 123;
            char *string_addr = (char *)my_integer;
            *string_addr = 3;

{Indeed, WeakTyping is often exhibited by unsafe memory accesses and crashes (segmentation faults and such). And DynamicTyping is well known for performing automatic conversions between 'types' whenever necessary; it is quite feasible to have DynamicTyping languages that restrict at time of variable assignment - though most choose to wait until the last possible moment.}


I've usually seen MultipleAssignment used to refer to something else:

 (A,B,C) = foo(bar);
This would be multiple assignment if A, B, C are variables and foo(bar) returns a 3-tuple.

I guess it's relevant that the C-style MultipleAssignment is mostly used for variable 'initialization', whereas the above sort of MultipleAssignment is more common to variables in a single-assignment language (where 'initialization' is somewhat counter-productive).

I, being a Pythonista, would call that "iterable unpacking".


MultipleAssignment is also a term used in the writings of DateAndDarwen to refer to the process of carrying out "several presumably interrelated single assignments as an atomic operation, without any integrity checking being done until all of the assignments in question have been executed."

For further explanation, see the relevant sections in TheThirdManifesto and AnIntroductionToDatabaseSystems books. Their RelationalLanguage TutorialDee provides explicit syntax for this, for example:

 a := b,
 b := c,
 c := d,
 p := q;
Or:
 UPDATE relvar1 (a := 1, b := c + 3),
 INSERT relvar2 UNION {relvar1, relvar3},
 relvar3 := relvar1 MINUS relvar2;
The semantics of multiple assignment are (as of the 3rd edition of TheThirdManifesto) given as follows:

Let MA be the multiple assignment A1, A2, ..., An where Ai is an individual assignment which assigns to exactly one target variable.

a. For i := 1 to n, expand any syntactic shorthands involved in Ai. (Note that typical database update operations like UPDATE, INSERT, and DELETE are syntactic shorthands for assignment to a variable, typically a RelVar.) After all such expansions, let MA take the form V1 := X1, V2 := X2, ..., Vz := Xz for some z >= n, where Vi is the name of some variable not defined in terms of any others and Xi is an expression of declared type the same as that of Vi.

b. Let p and q (1 <= p < q <= z) be such that Vp and Vq are identical and there is no r (r < p or p < r < q) such that Vp and Vr are identical. Replace Aq in MA by an assignment of the form Vq := WITH xp AS Vq : Xq (See TheThirdManifesto for the meaning of WITH) and remove Ap from MA. Repeat this process until no such pair p and q remains. Let MA now consist of the sequence U1 := Y1, U2 := Y2, ..., Um := Ym where each Ui is some Vj (1 <= i <= j <= m <= z).

c. For i := 1 to m, evaluate Yi. Let the result be yi.

d. For i := 1 to m, assign yi to Ui.


CategoryLanguageFeature


FebruaryTen


EditText of this page (last edited February 14, 2010) or FindPage with title or text search