Reason
  • Документация
  • Попробовать
  • API
  • Сообщество
  • Блог
  • Languages iconРусский
    • 日本語
    • English
    • Deutsch
    • Español
    • Français
    • 한국어
    • Português (Brasil)
    • Українська
    • 中文
    • 繁體中文
    • Помочь с переводом
  • GitHub

›Основы языка

Введение

  • Что и зачем

Установка

  • Установка
  • Плагины для редакторов и IDE

Основы языка

  • Обзор
  • Let Bindings
  • Primitives
  • Basic Structures
  • Types
  • Records
  • Variants
  • Options and nullability
  • Functions
  • Recursion
  • Деструктуризация
  • Pattern Matching
  • Mutable Bindings
  • Loops
  • Modules

Advanced Features

  • JSX
  • External
  • Исключения
  • Объекты

JavaScript

  • Взаимодействие с JavaScript
  • Шпаргалка по синтаксису
  • Pipe First
  • Промис
  • Библиотеки
  • Конвертация из JS

Дополнительно

  • Часто задаваемые вопросы (FAQ)
  • Дополнительные инструменты
Translate

Деструктуризация

Destructuring is a concise way to extract data from a structure. Destructuring can be used anywhere a variable name might appear.

Tuples

let pair = (1, "Hello");

/* Destructuring */
let (one, hello) = pair;

/* Prints: Hello */
print_endline(hello);

Records

Destructuring is very commonly used with records.

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

let alice = {
  name: "Alice",
  age: 42,
};

/* Destructuring */
let {name, age} = alice;

/* Prints: Alice */
print_endline(name);
  • Note: When destructuring a record the type needs to be in scope or the record needs to be explicitly annotated.

Renaming Record Fields

While destructuring record fields, it is possible to bind variable names that are different than the record field names:

let {name: legalName, age} = alice;

/* Prints: Alice */
print_endline(legalName);

Function Arguments

/* Without destructuring */
let hello = (person) => {
  print_endline("Hello " ++ person.name);
};

/* With destructuring */
let hello = ({name}) => {
  print_endline("Hello " ++ name);
};

/* With destructuring and aliasing */
let hello = ({name: legalName}) => {
  print_endline("Hello " ++ legalName);
};
← RecursionPattern Matching →
  • Tuples
  • Records
    • Renaming Record Fields
  • Function Arguments