It's dangerous to go without Typesafety, take this

@defuex

General

- from Dr.Axel Rauschmayer's book "Exploring ReasonML and functional programming"

Other Static Type Systems

Reason (OCaml) Type System

Focus of Reason

Semantics and Language Features

Types

Definition:

let goal = 10;

Explicitly written type:

let goal: int = 10;

Types

Wrapping expression and types:

let myInt = (5: int) + (4: int);

Function declaration with parameters and their types:

let add = (x: int, y: int) : int => x + y;

It's also possible to use a different name for a type:

type scoreType = int; let x: scoreType = 10;

Record

Record

Definition:

type person = {age: int, name: string};
let me: person = {age: 30, name: Tim};

New record created from old record:

let meNextYear = {...me, age: me.age + 1};

Make field of record mutable:

type person = {age: int, mutable name: string};

Variant

Variant

Definition:

type myResponseVariant =
  | Yes
  | No
  | PrettyMuch;

let areYouCrushingIt = Yes;

Constructor Arguments & Pattern Matching

Let's take following example to explain pattern matching

type payload =
  | BadResult(int)
  | GoodResult(string)
  | NoResult;

Constructor Arguments & Pattern Matching


let data = GoodResult("Product shipped!");

Constructor Arguments & Pattern Matching


let data = GoodResult("Product shipped!");

let message =
  switch (data) {
  | GoodResult(theMessage) => "Success! " ++ theMessage
  | BadResult(errorCode) => "Something's wrong.
  The error code is: " ++ string_of_int(errorCode)
  };

Constructor Arguments & Pattern Matching

Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
NoResult

Functions

Functions

Definition:

let greet = (name) => "Hello " ++ name;

or

let greetMore = (name) => {
  let part1 = "Hello";
  part1 ++ " " ++ name
};

Functions

Labeled Arguments:

let addCoordinates = (~x, ~y) => {
  /* use x and y here */
};
addCoordinates(~x=5, ~y=6);

Different argument name:

let drawCircle = (~radius as r, ~color as c) => {
  setColor(c);
  startAt(r, r);
};

Functions

Optional Labeled Arguments:

let drawCircle = (~color, ~radius=?, ()) => {
  setColor(color);
  switch (radius) {
  | None => startAt(1, 1)
  | Some(r_) => startAt(r_, r_)
  }
};

Functions

Currying:

let add = (x, y) => x + y; /* Semantically the same as following definition */ let add = (x) => (y) => x + y;

ReasonReact

Why Reason?

Code Demo

Getting Started

Getting Started

Use a project template

Just the beginning…

Thanks! :) @defuex

Join us in Vienna