Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's generally called <em>destructuring bind</em> in functional languages (which don't <em>have</em> assignments) and <em>destructuring assignment</em> in imperative languages.</p> <p>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 <em>Tuple unpacking</em>, <em>List unpacking</em> or <em>Sequence unpacking</em>, in Ruby, it works with Arrays (or objects that are convertible to an array) and is called <em>parallel assignment</em>.</p> <p>Destructuring bind can get arbitrarily complex. E.g. this (imaginary) bind</p> <pre><code>[Integer(a), b, 2, c] = some_array </code></pre> <p>would assign the first element of <code>some_array</code> to <code>a</code>, the second element to <code>b</code> and the fourth element to <code>c</code>, but <em>only</em> if the first element is an <code>Integer</code>, the third element is equal to <code>2</code> and the length is 4. So, this even incorporates some conditional logic.</p> <p>Destructuring bind is a subset of more general <em>pattern matching</em>, 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.)</p> <p>Here's the classical example of a <code>reverse</code> function in an imaginary language, written using pattern matching:</p> <pre><code>def reverse(l: List): List { match l { when [] { return [] } when [first :: rest] { return (reverse(rest) :: first) } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload