Quick StartTryGuideAPICommunityBlogGitHub

DestructuringSuggest an edit

"Destructuring" is a visually concise way of extracting fields from a data structure. You can use destructuring anywhere you'd normally use a variable.


The following binds variables: ten = 10, twenty = 20

let someInts = (10, 20);
let (ten, twenty) = someInts;

The following binds variables: name = "Guy", age = 30

type person = {name: string, age: int};
let somePerson = {name: "Guy", age: 30};
let {name, age} = somePerson;

When you pull out fields, you can optionally rename the fields. The following binds these instead: n = "Guy", a = 30.

let {name: n, age: a} = somePerson;

Destructuring also allows type annotations.

let (ten: int, twenty: int) = someInts;
let {name: (n: string), age: (a: int)} = somePerson;

Destructuring a functions' labeled arguments is also possible.

type person = {name: string, age: int};

let someFunction person::{name} => {
  /* you can use `name` here */

let otherFunction person::({name} as thePerson) => {
  /* you can use both `name` and the whole record as `thePerson` here */

Keep reading the section, pattern matching, for a crazier form of destructuring!

Tips & Tricks

Destructuring can make your code much more concise without requiring you to name intermediate variables. Do use them! But don't abuse them and make your code overly nested & terse.

If you're destructuring a record or a variant whose definition isn't in the current file, you need to explicitly annotate it. See here and here.