Skip to Main Content
ReasonML Quick Start Guide
book

ReasonML Quick Start Guide

by Raphael Rafatpanah, Bruno Joseph D'mello
February 2019
Beginner to intermediate content levelBeginner to intermediate
180 pages
4h 4m
English
Packt Publishing
Content preview from ReasonML Quick Start Guide

Pattern matching

We can use pattern matching on our person:

switch (person) {| ("Zoe", age) => {j|Zoe, $age years old|j}| _ => "another person"};

Let's use a record instead of a tuple for our person. Records are similar JavaScript objects except they're much lighter and are immutable by default:

type person = {  age: int,  name: string};let person = {  name: "Zoe",  age: 3};

We can use pattern matching on records too:

switch (person) {| {name: "Zoe", age} => {j|Zoe, $age years old|j}| _ => "another person"};

Like JavaScript, {name: "Zoe", age: age} can be represented as {name: "Zoe", age}.

We can create a new record from an existing one using the spread ( ... ) operator:

let person = {...person, age: person.age + 1};

Records require type definitions ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Reinventing the Organization for GenAI and LLMs

Reinventing the Organization for GenAI and LLMs

Ethan Mollick
Expert F# 4.0, Fourth Edition

Expert F# 4.0, Fourth Edition

Don Syme, Adam Granicz, Antonio Cisternino
Learn Type-Driven Development

Learn Type-Driven Development

Soumya Mukherjee, Yawar Amin, Kamon Ayeva

Publisher Resources

ISBN: 9781789340785Supplemental Content