Reason
  • ドキュメント
  • 試してみる
  • API
  • コミュニティ
  • ブログ
  • Languages icon日本語
    • English
    • Deutsch
    • Español
    • Français
    • 한국어
    • Português (Brasil)
    • Русский
    • Українська
    • 中文
    • 繁體中文
    • 翻訳を助ける
  • GitHub

›言語の基本

はじめに

  • What & Why

セットアップ

  • インストール
  • エディタのプラグイン

言語の基本

  • 概要
  • Let Bindings
  • Primitives
  • Basic Structures
  • Types
  • Records
  • Variants
  • Options and nullability
  • Functions
  • Recursion
  • 構造化代入
  • Pattern Matching
  • Mutable Bindings
  • Loops
  • Modules

Advanced Features

  • JSX
  • External
  • 例外
  • Object

JavaScript

  • Interop
  • Syntax Cheatsheet
  • Pipe First
  • Promise
  • ライブラリ
  • JS からの変換

Extra

  • よくある質問
  • Extra Goodies
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