Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Twitter
  • GitHub
  • Forum
Language Manual
Overview
  • Introduction
  • Installation
  • Editor Plugins
  • Migrate to ReScript Syntax
  • Try
Language Features
  • Overview
  • Let Binding
  • Type
  • Primitive Types
  • Tuple
  • Record
  • Object
  • Variant
  • Null, Undefined and Option
  • Array & List
  • Function
  • If-Else & Loops
  • Pipe
  • Pattern Matching / Destructuring
  • Mutation
    • Mutate Let-binding
    • Usage
    • Tip & Tricks
  • JSX
  • Exception
  • Lazy Values
  • Promise
  • Module
  • Import & Export
  • Reserved Keyword
JavaScript Interop
  • Embed Raw JavaScript
  • Shared Data Types
  • External (Bind to Any JS Library)
  • Bind to JS Object
  • Bind to JS Function
  • Import from / Export to JS
  • Bind to Global JS Values
  • JSON
  • Use Illegal Identifier Names
  • Generate Converters & Helpers
  • Browser Support & Polyfills
  • Interop Cheatsheet
Build System
  • Build System Overview
  • Build System Configuration
  • Interop with JS Build Systems
  • Build Performance
Guides
  • Converting from JS
  • Libraries
Extra
  • Newcomer Examples
  • Project Structure
  • FAQ
Docs / Language Manual / Mutation
Edit

You are currently looking at the v6.0 - v8.2 docs (Reason v3.6 syntax edition). You can find the latest manual page here.

(These docs are equivalent to the old BuckleScript docs before the ReScript rebrand)

Mutation

ReScript has great traditional imperative & mutative programming capabilities. You should use these features sparingly, but sometimes they allow your code to be more performant and written in a more familiar pattern.

Mutate Let-binding

Let-bindings are immutable, but you can wrap it with a ref, exposed as a record with a single mutable field in the standard library:

Reason (Old Syntax)ML (Older Syntax)JS Output
let myValue = ref(5);

Usage

You can get the actual value of a ref box through accessing its contents field:

Reason (Old Syntax)ML (Older Syntax)JS Output
let five = myValue.contents; // 5

We provide a syntax shortcut for myValue.contents: myValue^. Though we no longer encourage it.

Assign a new value to myValue like so:

Reason (Old Syntax)ML (Older Syntax)JS Output
myValue.contents = 6;

We provide a syntax sugar for this:

Reason (Old Syntax)ML (Older Syntax)JS Output
myValue := 6;

Note that the previous binding five stays 5, since it got the underlying item on the ref box, not the ref itself.

Note: you might see in the JS output tabs above that ref allocates an object. Worry not; local, non-exported refs allocations are optimized away.

Tip & Tricks

Before reaching for ref, know that you can achieve lightweight, local "mutations" through overriding let bindings.

Pattern Matching / DestructuringJSX

© 2024 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on