Reason
  • 문서
  • 해보기
  • API
  • 커뮤니티
  • 블로그
  • Languages icon한국어
    • 日本語
    • English
    • Deutsch
    • Español
    • Français
    • Português (Brasil)
    • Русский
    • Українська
    • 中文
    • 繁體中文
    • 번역 돕기
  • GitHub

›언어 기본

소개

  • What & Why

Setup

  • 설치
  • 에디터 플러그인

언어 기본

  • 개요
  • Let 바인딩
  • 원시 타입
  • 기본 자료구조
  • 타입
  • 레코드
  • Variant
  • Options and nullability
  • 함수
  • 재귀
  • 비구조화
  • 패턴 매칭
  • Mutable Bindings
  • 반복문
  • Modules

Advanced Features

  • JSX
  • 외부 접근
  • 예외
  • 오브젝트

JavaScript

  • 연동
  • 문법 치트시트
  • Pipe First
  • 프라미스
  • 라이브러리
  • JS에서 변환

추가 사항

  • 자주 물어보는 질문
  • 추가적으로 매력적인 것들
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

← 패턴 매칭반복문 →