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

Mutable Bindings

Quick overview: Refs

Refs allow mutable bindings in your program. They are a thin wrapper around a record with a mutable field called contents.

type ref('a) = {
  mutable contents: 'a,
};

There is syntax built into the language to work with ref structures.

  • ref(value) creates a ref with contents as value
  • x^ accesses the contents of the ref x
  • x := updates the contents of the ref x
let x = ref(10);
x := x^ + 10;
x := x^ + 3;
/* x^ is 23 */

If you prefer to avoid the ref syntax the following code is exactly equivalent to the syntax above:

let x = {contents: 10};
x.contents = x.contents + 10;
x.contents = x.contents + 3;
/* x.contents is 23 */

Note: Make sure you need mutable bindings before using them. The trivial example above should not use mutable bindings and instead should use: Binding Shadowing

← Pattern MatchingLoops →