/* list of strings */
let _ = ["example-1", "example-2", "example-3"];
/* Array of strings */
let _ = [|"example-1", "example-2", "example-3"|];
/* Stating the type of a Reason record */
type event = {
location: string,
years: list int
};
/* Making a Reason record */
{location: "Bratislava", years: [2017, 2018]};
/** Our first Reason function, already?
(Use ^ to join strings together) */
let rock song => Js.log ("We're all rocking to " ^ song);
/* Manually specifying types */
/* Use {j| ... |j} to interpolate strings */
let rock (song: string) (times: int) :string =>
{j|Rocked out to $(song) $(string_of_int times) times|j};
rock "Nad Tatrou sa blýska" 1; /* Invoke our function! */
/* Function with labelled arguments */
let rockLabelled ::songName ::times =>
{j|Rocked out to $(song) $(string_of_int times) times|j};
rockLabelled songName::"Nad Tatrou sa blýska" times::1; /* Invoke our function with labelled arguments! */
/* Making a ReasonReact component */
MyComponent.make foo::bar children::[] ()
/* Making a ReasonReact component with JSX */
<MyComponent foo={bar} />
/* A variant animal type */
type animal =
| Dog
| Cat
| Bird;
/* Pattern matching our custom animal variant type */
let feed pet =>
switch pet {
| Dog => "woof"
| Cat => "meow"
| Bird => "chirp"
};
/** Destructuring combines code flow and extracts values at the same time,
let's do it here with a list of strings */
let names = ["Daniel", "Jared", "Sean"];
switch names {
| [] => "No names!"
| [personOne] => "One person in list, named " ^ personOne
| [personOne, personTwo] => "Two people in list, both " ^ personOne ^ " and " ^ personTwo
| [personOne, _, personThree, ...rest] =>
"At least three people in the list, but we picked out " ^ personOne ^ " and " ^ personThree
};
/* Destructuring a record type */
type event = {
location: string,
years: list int
};
let event = {location: "Bratislava", years: [2017, 2018]};
let message = switch event {
| {location: "Bratislava", years} =>
"This event was in Bratislava for " ^ (string_of_int (List.length years))
| {location, years: [2018, 2019]} => "This event was in " ^ location ^ " for 2018 and 2019"
| event => "This is a generic event"
};
/* Binding to JavaScript libraries */
/* From https://github.com/reasonml-community/bs-moment/blob/master/src/MomentRe.re */
external alert : string => unit = "alert" [@@bs.val];
external imul : int => int => int = "Math.imul" [@@bs.val];
external reverse : array 'someKind => array 'someKind = "" [@@bs.send];
let identity: 'a => 'a => 'a = [%bs.raw {|function(x,y){/* Dangerous JavaScript! */ return x < y}|}];
alert "Bound successfully!";
imul 1 2;
reverse [|1, 2, 3|];
identity 1 2;