Function
Cheat sheet for the full function syntax at the end.
ReScript functions are declared with an arrow and return an expression, just like JS functions. They compile to clean JS functions too.
This declares a function and assigns to it the name greet
, which you can call like so:
Multi-arguments functions have arguments separated by comma:
For longer functions, you'd surround the body with a block:
If your function has no argument, just write let greetMore = () => {...}
.
Labeled Arguments
Multi-arguments functions, especially those whose arguments are of the same type, can be confusing to call.
You can attach labels to an argument by prefixing the name with the ~
symbol:
You can provide the arguments in any order:
The ~x
part in the declaration means the function accepts an argument labeled x
and can refer to it in the function body by the same name. You can also refer to the arguments inside the function body by a different name for conciseness:
As a matter of fact, (~radius)
is just a shorthand for (~radius as radius)
.
Here's the syntax for typing the arguments:
Optional Labeled Arguments
Labeled function arguments can be made optional during declaration. You can then omit them when calling the function.
When given in this syntax, radius
is wrapped in the standard library's option
type, defaulting to None
. If provided, it'll be wrapped with a Some
. So radius
's type value is None | Some(int)
here.
More on option
type here.
Note for the sake of the type system, whenever you have an optional argument, you need to ensure that there's also at least one positional argument (aka non-labeled, non-optional argument) after it. If there's none, provide a dummy unit
(aka ()
) argument.
Signatures and Type Annotations
Functions with optional labeled arguments can be confusing when it comes to signature and type annotations. Indeed, the type of an optional labeled argument looks different depending on whether you're calling the function, or working inside the function body. Outside the function, a raw value is either passed in (int
, for example), or left off entirely. Inside the function, the parameter is always there, but its value is an option (option<int>
). This means that the type signature is different, depending on whether you're writing out the function type, or the parameter type annotation. The first being a raw value, and the second being an option.
If we get back to our previous example and both add a signature and type annotations to its argument, we get this:
The first line is the function's signature, we would define it like that in an interface file (see Signatures). The function's signature describes the types that the outside world interacts with, hence the type int
for radius
because it indeed expects an int
when called.
In the second line, we annotate the arguments to help us remember the types of the arguments when we use them inside the function's body, here indeed radius
will be an option<int>
inside the function.
So if you happen to struggle when writing the signature of a function with optional labeled arguments, try to remember this!
Explicitly Passed Optional
Sometimes, you might want to forward a value to a function without knowing whether the value is None
or Some(a)
. Naively, you'd do:
This quickly gets tedious. We provide a shortcut:
This means "I understand radius
is optional, and that when I pass it a value it needs to be an int
, but I don't know whether the value I'm passing is None
or Some(val)
, so I'll pass you the whole option
wrapper".
Optional with Default Value
Optional labeled arguments can also be provided a default value. In this case, they aren't wrapped in an option
type.
Recursive Functions
ReScript chooses the sane default of preventing a function to be called recursively within itself. To make a function recursive, add the rec
keyword after the let
:
A simple recursive function may look like this:
Recursively calling a function is bad for performance and the call stack. However, ReScript intelligently compiles tail recursion into a fast JavaScript loop. Try checking the JS output of the above code!
Mutually Recursive Functions
Mutually recursive functions start like a single recursive function using the
rec
keyword, and then are chained together with and
:
Uncurried Function
ReScript's functions are curried by default, which is one of the few performance penalties we pay in the compiled JS output. The compiler does a best-effort job at removing those currying whenever possible. However, in certain edge cases, you might want guaranteed uncurrying. In those cases, put a dot in the function's parameter list:
If you write down the uncurried function's type, you'll add a dot there as well.
Note: both the declaration site and the call site need to have the uncurry annotation. That's part of the guarantee/requirement.
This feature seems trivial, but is actually one of our most important features, as a primarily functional language. We encourage you to use it if you'd like to remove any mention of Curry
runtime in the JS output.
Async/Await (from v10.1)
Just as in JS, an async function can be declared by adding async
before the definition, and await
can be used in the body of such functions.
The output looks like idiomatic JS:
The return type of getUser
is inferred to be promise<string>
.
Similarly, await getUserName(userId)
returns a string
when the function returns promise<string>
.
Using await
outside of an async
function (including in a non-async callback to an async function) is an error.
Ergonomic error handling
Error handling is done by simply using try
/catch
, or a switch with an exception
case, just as in functions that are not async.
Both JS exceptions and exceptions defined in ReScript can be caught. The compiler takes care of packaging JS exceptions into the builtin JsError
exception:
The ignore() Function
Occasionally you may want to ignore the return value of a function. ReScript provides an ignore()
function that discards the value of its argument and returns ()
:
Tips & Tricks
Cheat sheet for the function syntaxes:
Declaration
RES// anonymous function
(x, y) => 1
// bind to a name
let add = (x, y) => 1
// labeled
let add = (~first as x, ~second as y) => x + y
// with punning sugar
let add = (~first, ~second) => first + second
// labeled with default value
let add = (~first as x=1, ~second as y=2) => x + y
// with punning
let add = (~first=1, ~second=2) => first + second
// optional
let add = (~first as x=?, ~second as y=?) => switch x {...}
// with punning
let add = (~first=?, ~second=?) => switch first {...}
With Type Annotation
RES// anonymous function
(x: int, y: int): int => 1
// bind to a name
let add = (x: int, y: int): int => 1
// labeled
let add = (~first as x: int, ~second as y: int) : int => x + y
// with punning sugar
let add = (~first: int, ~second: int) : int => first + second
// labeled with default value
let add = (~first as x: int=1, ~second as y: int=2) : int => x + y
// with punning sugar
let add = (~first: int=1, ~second: int=2) : int => first + second
// optional
let add = (~first as x: option<int>=?, ~second as y: option<int>=?) : int => switch x {...}
// with punning sugar
// note that the caller would pass an `int`, not `option<int>`
// Inside the function, `first` and `second` are `option<int>`.
let add = (~first: option<int>=?, ~second: option<int>=?) : int => switch first {...}
Application
RESadd(x, y)
// labeled
add(~first=1, ~second=2)
// with punning sugar
add(~first, ~second)
// application with default value. Same as normal application
add(~first=1, ~second=2)
// explicit optional application
add(~first=?Some(1), ~second=?Some(2))
// with punning
add(~first?, ~second?)
With Type Annotation
RES// labeled
add(~first=1: int, ~second=2: int)
// with punning sugar
add(~first: int, ~second: int)
// application with default value. Same as normal application
add(~first=1: int, ~second=2: int)
// explicit optional application
add(~first=?Some(1): option<int>, ~second=?Some(2): option<int>)
// no punning sugar when you want to type annotate
Standalone Type Signature
RES// first arg type, second arg type, return type
type add = (int, int) => int
// labeled
type add = (~first: int, ~second: int) => int
// labeled
type add = (~first: int=?, ~second: int=?, unit) => int
In Interface Files
To annotate a function from the implementation file (.res
) in your interface file (.resi
):
let add: (int, int) => int
The type annotation part is the same as the previous section on With Type Annotation.
Don't confuse let add: myType
with type add = myType
. When used in .resi
interface files, the former exports the binding add
while annotating it as type myType
. The latter exports the type add
, whose value is the type myType
.