Note: information on this page refers to Ceylon 1.2, not to the current release.

destructuring

Destructuring allows the specification of several values at once, when their value is extracted from a Tuple or Entry.

Usage

// destructure an Entry
value key->item = entry;

[Float, Float, Float] point = [1.0, 1.0, 1.0];
// declare three values extracting the valee of each from the tuple
value [x, y, z] = point;

[Float, Float, Float]? maybePoint = point;
// in conjunction with the exists condition
if (exists [x, y, z] =  maybePoint) {
    // ...
}

[Float+] floats = [0.0, 1.0, 2.0, 3.0];
// destructure with spread
value [first, *rest] = floats;

[Float, Float, [String, Icon]] labelledPoint = ...;
// nested destructure
value [x, y, [name, icon]] = labelledPoint;

Description

Destructuring specification is supported:

  • In value declarations (value [x, y] = point),
  • In [for iteration]](../../statement/for/) variables (for[x, y] in points) { ... }),
  • In condition lists (if (exists [x, y]=maybePoint) { ... }, assert(exists [x, y]=maybePoint);, assert(nonempty [first, *rest]=seq); etc),
  • In let expressions (let ([x, y] = point) sqrt(x^2+y^2)),
  • In comprehensions ({ for[x, y] in points) sqrt(x^2+y^2) }),

It is not supported with types other than Tuple or Entry.

See also