Skip to content

Latest commit

 

History

History
41 lines (32 loc) · 992 Bytes

mutable-bindings.md

File metadata and controls

41 lines (32 loc) · 992 Bytes
title
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