weeks and it shows… [] + []; // evaluates to "" ➞ weird language features we sometimes need to deal with undefined is not a function ➞ issues frequently slip into production code causing runtime errors (mainly due to JS being an interpreted lang with dynamic typing)
name: "John", role: "reader" }; if (user.role = "admin") { // do something dangerous } // could be part of a larger code base // could even happen to experienced programmers: // copy/paste, mistyping, cat on keyboard, etc.
{ name: "Jane", city: { name: "Rome" } }; console.log(john.getCoordinates()); // another famous TypeError in JavaScript: // <something> is not a function // often seen as --> undefined is not a function Example: runtime error (TypeError)
devs for easy adoption) BuckleScript for JS output (generates readable, optimized JavaScript) OCaml Semantics (powerful type system and language features)
type system) great interop with JavaScript (also work with npm/yarn) provide powerful functional programming paradigms first class support for React Reason's syntax makes this approachable for JavaScript devs
a + b; type person = { name: string, mutable role: string, }; let user = {name: "John", role: "reader"}; let makeAdmin = someone => someone.role = "admin"; let printRole = somebody => { print_endline(somebody.name ++ " has this role:"); print_endline(somebody.role); }; often no need to annotate types — they are inferred automatically!
typing. (often no need to annotype types as they're inferred automatically) The type system is sound (incorrectly typed program won't compile). (removes whole categories of bugs & prevents runtime errors) Reason code has 100% type coverage. (thus code is implicitly documented & this is great for refactorings)
name: string, role: string }; /* a role might be assigned any string value */ let user = {name: "John", role: "Reader"}; if (user.role == "Admin") { print_endline("now we do something dangerous"); }; Instead of allowing any string value for role, it would be great if we could explicitly define all its variants (Reader, Admin, etc.).
*/ | Reader | Admin; type person = { name: string, role, }; let user = {name: "John", role: Reader}; if (user.role == Admin) { print_endline("now we do something dangerous"); }; Type role is called a Variant consisting of distinct construtors/tags. Now we can only use Reader and Admin when working with roles!
how useful this will be for refactorings. type role = | Reader | Editor | Admin; type person = { name: string, role, }; let's extend the variant with an Editor role…
matching, there are no null errors in Reason! type role = | Reader | Editor | Admin; type person = { name: string, role: option(role) }; what if a person's role could be optional? let user = {name: "John", role: Some(Reader)}; let user2 = {name: "Joe", role: None}; let messageForUser = user => switch (user.role) { | Some(Reader) => "Go away!" | Some(Editor) => "Your password?" | Some(Admin) => "Please enter..." | None => "How dare you asking..." };
is a module, no need to require/import anything ✴great tooling (code formatting with refmt, REPL (rtop), friendly error messages) ✴blazing fast compile times (it's said to be much faster than TypeScript) ✴great interop with JavaScript, output is readable JS with dead code elimination ✴compiles to native code (possibly future React Native without JavaScript bridge?) ✴first level support for React (via ReasonReact: https://reasonml.github.io/reason-react/) ✴level up and learn about advanced language concepts (GADT, Phantom Types,…)
rebuild of the Reason part of the codebase is ~2s (a few hundreds of files), incremental build (the norm) is <100ms on average.” “Messenger used to receive bugs reports on a daily basis; since the introduction of Reason, there have been a total of 10 bugs (that's during the whole year, not per week)!” https://reasonml.github.io/blog/2017/09/08/messenger-50-reason.html
is a syntax upgrade tool) - Some new concepts to learn (might payoff in future) - You lose some flexibility (the price to pay for having static typing) - Don't expect that your code will be completely free from defects! - OCaml standard lib fragmentation (work in progress) - unicode not fully supported (workaround with BuckleScript possible) - async/await story still being worked on
JavaScript code at dev time. • We have ESLint and many static typing tools to choose from. • Reason is a promising solution covering linting and static typing: ✓powerful type system (inference, soundness, coverage, …) ✓super useful extra features (variant, pattern matching, …) ✓enable JavaScript devs get started (syntax, toolchain, interop, …)
‣ Book, free to read online: http://reasonmlhub.com/exploring-reasonml/ ‣ Articles/Videos: https://reasonml.github.io/docs/en/articles-and-videos.html ‣ Use Reason in some part of your app: ‣ Install: https://reasonml.github.io/docs/en/global-installation.html ‣ Convert JS: https://reasonml.github.io/docs/en/converting-from-js.html (check in both JS and Reason code into your VCS)