- expression-oriented programming one of the great advances of FP
- expressions plug together like legos, making more malleable programming experience in-the-small
Write in an expression-oriented style, scoping variables as locally as possible:
let x = do {
let tmp = f();
tmp * tmp + 1
};
Use conditional statements as expressions, instead of awkward nested ternaries:
let x = do {
if (foo()) { f() }
else if (bar()) { g() }
else { h() }
};
Especially nice for templating languages like JSX:
return (
<nav>
<Home />
{
do {
if (loggedIn) {
<LogoutButton />
} else {
<LoginButton />
}
}
}
</nav>
)
- key refactoring principles:
do { <expr>; }
equivalent to<expr>
(do { <stmt> };)
equivalent to{ <stmt> }
- this semantic transparency is demonstrated by the semantics:
- Return the result of evaluating Body.
How to avoid either parsing conflict in statement context with do
-while
, or dangling-else type of ambiguity:
do do f(); while (x);
I have several alternatives I intend to explore here.
@Alexsey That's not entirely true, essentially I'd like semantics similar to arrow functions but that currently don't exist in the language essentially I'd want the following to happen:
The only things I'm undecided on is how I'd want to see
yield
/await
in a plain do expression:One: Simply transfer outer control of
await
/yield
to the do expression:Two: do is like an iife in almost every way except with special returning rules (although =>* doesn't have any proposal linked so I'd wait for this behaviour):
Three: Neither of the above, simply shorthand for immediately invoked normal arrow functions: