01
Erste Schritte
Hello World
ReasonML kompiliert über BuckleScript zu JavaScript.
reasonml
Js.log("Hello, World!");Let
let-Bindings enden mit Semikolons.
reasonml
let x = 10;
let y = 20;
Js.log(x + y);Funktionen
Arrow-Funktionen wie JS/ES6.
reasonml
let add = (a, b) => a + b;
Js.log(add(3, 4));Typen
Record-Typen mit JS-ähnlicher Syntax.
reasonml
type person = {name: string, age: int};
let p = {name: "Alice", age: 30};
Js.log(p.name);02
Features
Variant
Variants mit switch für Pattern-Matching.
reasonml
type result =
| Ok(int)
| Err(string);
let r = Ok(42);
switch (r) {
| Ok(v) => Js.log(v)
| Err(e) => Js.log(e)
};Option
Der Option-Typ für nullable Werte.
reasonml
let safeDiv = (a, b) =>
if (b === 0) { None } else { Some(a / b) };Pipe
Der Pipe-first-Operator |>.
reasonml
[1, 2, 3, 4, 5]
|> List.filter(x => x mod 2 === 0)
|> List.map(x => x * 2)
|> Js.log;Was this helpful?