A moderately deep look at new language with OCaml roots. Is there a reason to be hyped about it? Is it mature enough to be considered production ready?
What does it offer over JS, Flow or other compiled to JS languages?
2. WHAT IS REASON
New syntax, more familiar to JavaScript developers
JavaScript and Native compilation targets
Set of more accessible documentations, curated libraries and utilities
Umbrella project that provides a curated layer
for OCaml language.
17. DATA TYPES
Records
type company = {name: string, size: int};
let gp = {name: "Grand Parade", size: 600};
light 💥 immutable by default 💥 fast 💥 less flexible than objects
34. BUCKLESCRIPT
💥 human readable output 💥
type company = {
name: string,
size: int
};
let gp = {
name: "Grand Parade",
size: 250
};
let gpNext = {
...gp,
size: 600
};
'use strict';
var gpNext = /* record */[
/* name */"Grand Parade",
/* size */600
];
var gp = /* record */[
/* name */"Grand Parade",
/* size */250
];
exports.gp = gp;
exports.gpNext = gpNext;
35. BUCKLESCRIPT
💥 optimizations 💥
type person = {
name: string,
score: int
};
let joe = {
name: "Joe",
score: 95
};
let joeGreeting =
if (joe.score + 10 > 20) {
"Hello!"
} else {
"Hey!"
};
'use strict';
var joe = /* record */[
/* name */"Joe",
/* score */95
];
var joeGreeting = "Hello!";
exports.joe = joe;
exports.joeGreeting = joeGreeting;
43. BUCKLESCRIPT
💥 small output 💥
module IntMap =
Map.Make {
type t = int;
let compare (x: int) y => compare x y;
};
let test () => {
let m = ref IntMap.empty;
let count = 1000000;
for i in 0 to count {
m := IntMap.add i i !m
};
for i in 0 to count {
ignore (IntMap.find i !m)
}
};
test ();
var Immutable = require('immutable');
var Map = Immutable.Map;
var m = new Map();
function test() {
var count = 1000000;
for(var i = 0; i < count; ++i) {
m = m.set(i, i);
}
for(var j = 0; j < count; ++j) {
m.get(j);
}
}
test();
44. BUCKLESCRIPT
💥 small output 💥
module IntMap =
Map.Make {
type t = int;
let compare (x: int) y => compare x y;
};
let test () => {
let m = ref IntMap.empty;
let count = 1000000;
for i in 0 to count {
m := IntMap.add i i !m
};
for i in 0 to count {
ignore (IntMap.find i !m)
}
};
test ();
execution time: 3415 ms
size: 55.3 kB (minified)
45. BUCKLESCRIPT
💥 small output 💥
execution time: 3415 ms
size: 55.3 kB (minified)
execution time: 1186 ms
size: 899 B (minified)
49. SAFETY
type company = {
name: string,
size: int
};
let printCompanyDetails c =>
c.name ^ ", has " ^ string_of_int c.size ^ " employees";
*in the context of JS
52. SAFETY
printCompanyDetails {
name: "Grand Parade",
size: 250,
location: "Cracow"
};
File "test.re", line 11, characters 54-62:
Error: This record expression is expected to have type company
The field location does not belong to type company
53. SAFETY
File "test.re", line 7, characters 2-42:
Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
(Black|Green)
type colorName = Blue | Black | Green;
let toHex color =>
switch color {
| Blue => "#0000ff"
};
54. SAFETY
let toHex color =>
switch color {
| Blue => "#0000ff"
| Black => "#000000"
| Green => "#00ff00"
};
56. SAFETY
let displayState state => {
print_string state.data;
}
option type
File "test.re", line 3, characters 39-49:
Error: This expression has type string option
but an expression was expected of type string
57. SAFETY
let displayState state => {
let str =
switch state.data {
| None => "No data!"
| Some data => data
};
print_string str
};
option type
64. WHAT WE DIDN'T TALK ABOUT
Modules
Objects and OOP
React Reason
Async
Exceptions
65. IS IT READY?
Small number of libraries / products written in Reason
24 repositories for library bindings
16 questions on StackOverflow
Big changes to the language to come
Hard to get into documentation (on OCaml side)
Native workflow WIP
66. LEARNING RESOURCES
Courses
Programming Languages, part A [coursera]
Books
Any OCaml book thanks to Reason Tools
Real World OCaml - the most popular one
Docs
Reason 👍 Bucklescript 👍 Reason React
Tutorials / blogs
Jared Forsyth jaredforsyth.com 👍 James Friend jamesfriend.com.au