Teaching ReasonML
SWBAT: Write a todo list in ReasonML?
Why Reason?
- Compiles to JS, Native mac/android/ios apps, bytecode, even a mini operating system
- ReactNative FRD someday
- The syntax by the creator of React + a 20 year old language used to power mission critical financial systems
- Types without the typing (always inferred, 100% coverage)
- Looks like JS 🤗
- Facebook backs it
- Gradual typing 2.0
- λ Functional Programming by default, Imperative and Object-oriented for pragmatism reasons
- excels at handling inconsistency
- much more!
Basic Setup:
Install build system
# provides the BuckleScript compiler
npm install -g bs-platform
# Provides integration with VScode or whatever
npm install -g reason-cli@3.1.0-darwin
Make a basic reason app:
# creates project my-first-app/
# ompiles to src/Demo.bs.js using BuckleScript compiler
# and runs the demo using NodeJS
bsb -init my-first-app -theme basic-reason
cd my-first-app
npm run build
node src/Demo.bs.js
Make a ReasonReact app:
bsb -init my-react-app -theme react.
Basic types
/* immutable variable assigned to an int */
let a = 1;
/* immutable variable assigned to an float */
let b = 1.0;
Lists
Lists are linked lists they don't have random-access (you can't get the middle of them efficiently) but they are cheaper to add items to. Makes less of a difference in JS.
let c = [1, 2, 3];
They don't allow different types in the same thing.
Tuples
tuples are like elixir tuples. They use ()
's though
kind of like fixed length, polymorphic arrays. Or a struct were you're too lazy to name the fields.
Arrays
arrays have a different syntax and they allow random access
let d = [|1, 2, 3|];
This defines a struct. They have a fixed number of fields. They all must be present.
type person = {
name: string,
age: string
};
let gage = {name: "gage"}
Minting new types
GOTO 2018 • Unconditional Code • Michael Feathers - YouTube
Don't be "stringly" typed...
Ode to the overloaded string...
often used for
- a unique id:
var BLUE_COLOR = "blue"
- an identifier into a data structure:
var BLUE = "blue";
var RED = "red";
var colors = [BLUE, RED]
- the name of an object field:
person["age"] = 24
- an enum:
if (audio.canPlayType() === 'probably') {...}
(yes this is an actual DOM API) - other crazy patterns you'll soon find horrible, after getting used to Reason's alternatives.
Backlinks: