views:

165

answers:

7

I need to know a correct name for this cool feature that some languages provide.

FYI: In some languages it is possible to do a multiple assignments by assigning a structure of values to a structure of "variables". In the example in the question title it assigns "foo" to foo and "bar" to bar.

+2  A: 

Mozilla calls it destructuring assignment. In Python, it's sequence unpacking; tuple unpacking is a common special case.

Matthew Flaschen
+2  A: 

If you view the right hand side as a tuple, one could view the assignment as a kind of Tuple Unpacking.

Ani
+6  A: 

In Python it is known as list or sequence unpacking: http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences

my_list = ["foo", "bar"]
foo, bar = my_list
Adam Vandenberg
+4  A: 

It's called parallel assignment in Ruby and other languages.

yjerem
+3  A: 

Perl and PHP call it list assignment

Perl:

my ($foo, $bar, $baz) = (1, 2, 3);

PHP:

list($foo, $bar, $baz) = array(1, 2, 3);
bemace
+11  A: 

It's generally called destructuring bind in functional languages (which don't have assignments) and destructuring assignment in imperative languages.

Some languages provide subsets of that feature and then call it something different. For example, in Python it works with Tuples, Lists or Sequences and is called Tuple unpacking, List unpacking or Sequence unpacking, in Ruby, it works with Arrays (or objects that are convertible to an array) and is called parallel assignment.

Destructuring bind can get arbitrarily complex. E.g. this (imaginary) bind

[Integer(a), b, 2, c] = some_array

would assign the first element of some_array to a, the second element to b and the fourth element to c, but only if the first element is an Integer, the third element is equal to 2 and the length is 4. So, this even incorporates some conditional logic.

Destructuring bind is a subset of more general pattern matching, which is a standard feature of functional languages like Haskell, ML, OCaml, F#, Erlang and Scala. The difference is that destructuring bind only lets you take apart a structure and bind its components to variables, whereas pattern matching also matches on values inside those structures and lets you make decisions and in particular lets you run arbitrary code in the context of the bindings. (You can see the above imaginary bind as half-way in between destructuring bind and pattern matching.)

Here's the classical example of a reverse function in an imaginary language, written using pattern matching:

def reverse(l: List): List {
  match l {
    when []              { return [] }
    when [first :: rest] { return (reverse(rest) :: first) }
  }
}
Jörg W Mittag
Actually, in Python, it works for any sequence.
Matthew Flaschen
@Matthew Flaschen: Thanks. I added *Sequence Unpacking* and dropped the word "only" from the description.
Jörg W Mittag
I should have been more clear in both my answer (updated) and comment. Tuples and lists are particular kinds of sequence in Python, so tuple unpacking and list unpacking are just special cases of sequence unpacking.
Matthew Flaschen
+2  A: 

In Erlang it's ... well, it's not assignment, it's pattern matching (seeing as there is no assignment, as such, in Erlang).

$ erl
Erlang R14B (erts-5.8.1) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:true]

Eshell V5.8.1  (abort with ^G)
1> [H1, H2, H3| Rest] = [1,2,3,4,5].
[1,2,3,4,5]
2> H1.
1
3> H2.
2
4> H3.
3
5> Rest.
[4,5]

Why is it called "pattern matching"? Because it actually is matching patterns. Look:

6> [1,2,3,4,A] = [1,2,3,4,5].
[1,2,3,4,5]
7> A.
5
8> [1,2,3,4,A] = [1,2,3,4,6].
** exception error: no match of right hand side value [1,2,3,4,6]

In the first one we did what effectively amounts to an assertion that the list would start with [1,2,3,4] and that the fifth value could be anything at all, but please bind it into the unbound variable A. In the second one we did the same thing except that A is now bound so we're looking explicitly for the list [1,2,3,4,5] (because A is now 5).

JUST MY correct OPINION