let myFun = (x, y) => {
let doubleX = x + x;
let doubleY = y + y;
doubleX + doubleY
};
Currying
JavaScript
Reason
let add = a => b => a + b
let add = (a, b) => a + b
Both JavaScript and Reason support currying, but Reason currying is built-in and
optimized to avoid intermediate function allocation and calls, whenever
possible.
* This will cause the compiler to warn that not all cases are handled, because
data could be of length other than 2. Better switch to pattern-matching
instead.
Loop
JavaScript
Reason
for (let i = 0; i <= 10; i++) {...}
for (i in 0 to 10) {...}
for (let i = 10; i >= 0; i--) {...}
for (i in 10 downto 0) {...}
while (true) {...}
Same
JSX
JavaScript
Reason
<Foo bar=1 baz="hi" onClick={bla} />
Same
<Foo bar=bar />
<Foo bar /> *
<input checked />
<input checked=true />
No children spread
<Foo>...children</Foo>
* Note the argument punning when creating elements.
Exception
JavaScript
Reason
throw new SomeError(...)
raise(SomeError(...))
try {a} catch (Err) {...} finally {...}
try (a) { | Err => ...} *
* No finally.
Blocks
In Reason, "sequence expressions" are created with {} and evaluate to their
last statement. In JavaScript, this can be simulated via an immediately-invoked
function expression (since function bodies have their own local scope).
JavaScript
Reason th>
let res = (function() {
const x = 23;
const y = 34;
return x + y;
})();