Docs / Language Manual / PrimitiveTypes

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

Primitive Types

ReScript comes with the familiar primitive types like string, int, float, etc.

String

ReScript strings are delimited using double quotes (single quotes are reserved for the character type below).

Reason (Old Syntax)ML (Older Syntax)JS Output
 
let greeting = "Hello world!";
let multilineGreeting = "Hello
 world!";

To concatenate strings, use ++:

Reason (Old Syntax)ML (Older Syntax)JS Output
 
let greetings = "Hello " ++ "world!";

String Interpolation

There's a special syntax for string that allows

  • multiline string just like before

  • Interpolation

Reason (Old Syntax)ML (Older Syntax)JS Output
 
let greeting = {j|Hello
World
$(name)
|j}

This is just like JavaScript's backtick string interpolation.

Usage

See the familiar Js.String API in the API docs. Since a ReScript string maps to a JavaScript string, you can mix & match the string operations in both standard libraries.

Tips & Tricks

You have a good type system now! In an untyped language, you'd often overload the meaning of string by using it as:

  • a unique id: var BLUE_COLOR = "blue"

  • an identifier into a data structure: var BLUE = "blue" var RED = "red" var colors = [BLUE, RED]

  • the name of an object field: person["age"] = 24

  • an enum: if (audio.canPlayType() === 'probably') {...} (ಠ_ಠ)

  • other crazy patterns you'll soon find horrible, after getting used to ReScript's alternatives.

The more you overload the poor string type, the less the type system (or a teammate) can help you! ReScript provides concise, fast and maintainable types & data structures alternatives to the use-cases above (e.g. variants, in a later section).

Char

ReScript has a type for a string with a single letter:

Reason (Old Syntax)ML (Older Syntax)JS Output
 
let firstLetterOfAlphabet = 'a';

Note: Char doesn't support Unicode or UTF-8 and is therefore not recommended.

To convert a String to a Char, use "a".[0]. To convert a Char to a String, use String.make(1, 'a').

Regular Expression

ReScript regular expressions compile cleanly to their JavaScript counterpart:

Reason (Old Syntax)ML (Older Syntax)JS Output
 
let r = [%re "/b/g"];

A regular expression like the above has the type Js.Re.t. The Js.Re module contains the regular expression helpers you have seen in JS.

Boolean

A ReScript boolean has the type bool and can be either true or false. Common operations:

  • &&: logical and.

  • ||: logical or.

  • !: logical not.

  • <=, >=, <, >

  • ==: structural equal, compares data structures deeply: (1, 2) == (1, 2) is true. Convenient, but use with caution.

  • ===: referential equal, compares shallowly. (1, 2) === (1, 2) is false. let myTuple = (1, 2); myTuple === myTuple is true.

  • !=: structural unequal.

  • !==: referential unequal.

ReScript's true/false compiles into a JavaScript true/false.

Integers

32-bits, truncated when necessary. We provide the usual operations on them: +, -, *, /, etc. See Js.Int for helper functions.

Careful when you bind to JavaScript numbers! Long ones might be truncated. Bind JS number (especially Dates) as float instead.

Floats

Float requires other operators: +., -., *., /., etc. Like 0.5 +. 0.6. See Js.Float for helper functions.

Unit

The unit type has a single value, (). It compiles to JavaScrit's undefined. It's a dummy type used as a placeholder in various places. You won't need it until you see it.