Docs / Language Manual / Primitive Types
Edit

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).

ReScriptJS Output
let greeting = "Hello world!"
let multilineGreeting = "Hello
 world!"

To concatenate strings, use ++:

ReScriptJS Output
let greetings = "Hello " ++ "world!"

String Interpolation

There's a special syntax for string that allows

  • multiline string just like before

  • no special character escaping

  • Interpolation

  • Proper unicode handling

ReScriptJS Output
let name = "Joe"

let greeting = `Hello
World
👋
${name}
`

This is just like JavaScript's backtick string interpolation, except without needing to escape special characters.

For interpolation, you'll have to convert the binding (name in the example) into a string if it isn't one. If you want the interpolation to implicitly convert a binding into a string, prepend a j:

ReScriptJS Output
let age = 10
let message = j`Today I am $age years old.`

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:

ReScriptJS 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 String.get("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:

ReScriptJS 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.

Be careful when you bind to JavaScript numbers! Since ReScript integers have a much smaller range than JavaScript numbers, data might get lost when dealing with large numbers. In those cases it’s much safer to bind the numbers as float. Be extra mindful of this when binding to JavaScript Dates and their epoch time.

To improve readability, you may place underscores in the middle of numeric literals such as 1_000_000. Note that underscores can be placed anywhere within a number, not just every three digits.

Floats

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

As with integers, you may use underscores within literals to improve readability.

Unit

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