- 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.
@ivan-kleshnin This happens because
do-expressions
must strictly be in expression position, it's the same reason why you can't write an immediately invoked function expression without wrapping it brackets:Of course there's no spec text yet so it could still happen, Babel's parser (and v8's experimental implementation) are thus just guesses as to what the exact grammar might be, if there's no
async
variant I don't really see the purpose of having it in statement position when you could just use a block:Although if there's an
async
variant I think it'd be rather nice for top-level execution e.g.: