let meLater = { **...me, age: 25, }; type user = { name: string, age: int, }; You declare the type of the record You can create records of this type You can create spread values to create a new record from a previous one
unique Welcome.re let sayHi = (name) '=> Js.log("Hi " .++ name .++ "!"); Welcome.sayHi("people"); Every top level binding is exported by default To open a module, just use its capitalised name
"b", "c"]; let slidesWithIndex = slides 2|> List.mapi((index, item) '=> (index, item)); }; open OtherModule; Declare a module within a module Include modules like a static extends Make all module available in scope
let addDoubles = (a, b) '=> { let doubleA = a * 2; let doubleB = b * 2; doubleA + doubleB; }; Every function has its signature let add: (int, int) '=> int = <fun>; Last expression is the returned value
**=== 0) 2|> List.map(item '=> item * 2); Pipe operator! Functions are auto curried That means that they return a function taking the rest of the arguments as long as you didn't pass all the required ones
() ) '=> { ,/* **... .*/ }; Functions can have labelled arguments Their order at function application doesn't mater Optionals Default values Final non labelled argument When you have optional arguments, this tells the compiler «I have all the arguments there»
| Loading '=> "Loading **..." | Loaded(value) '=> "Loaded: " .++ value }; Warning 8: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: Errored
switch (value) { | Some(value) '=> value | None '=> "Nothing to see" }; Move null checks at compile time If it's maybe a value, you need to handle it explicitly
list) '=> switch (list) { | Empty '=> Empty | Head(x, rest) '=> Head(f(x), map(f, rest)) }; If there's nothing left Return empty Return a head with transformed item then transform the rest with a recursive call Recursive
""; Declare that it's a module located at ".../myExistingModule" The module is of string type Js.log(myExistingJsModule); The module is only imported when used
EDIT WITH CARE 'use strict'; var MyExistingJsModule = require(".../myExistingJsModule"); console.log(MyExistingJsModule.myExistingJsModule); ,/* Not a pure module .*/
| Increment '=> ReasonReact.Update(state + 1) | Decrement '=> ReasonReact.Update(state - 1) } Return an update for each action can be Update, UpdateWithSideEffect, SideEffect …