
Proposal: “Statements as Expressions” using do - steriodabsuer
https://ponyfoo.com/articles/proposal-statements-as-expressions-using-do
======
couchand
Help me out if I'm missing something important, but this doesn't really seem
to add much except for the conceptual overhead of another control structure.

The examples given in the article are as clear or clearer without the do
syntax. Compare:

    
    
        [1, -1, -0.5].map(x => do { if (x > 0) { x * 10 } else { -x * 10 } })
    

to

    
    
        [1, -1, -0.5].map(x => { if (x > 0) { return x * 10 } else { return -x * 10 } })
    

as well as

    
    
        var data = do {
          const data = pullSomeData()
          doSomethingElse() // sideEffect
          data
        }
    

versus

    
    
        const data = pullSomeData()  // or `var` if you wish
        doSomethingElse() // sideEffect
    

Perhaps a more significant example is warranted here. My suspicion is that if
you get code complicated enough to justify a `do` block, it's complicated
enough to justify a function that can be independently tested.

~~~
acjohnson55
I agree that the examples aren't particularly motivating. But I think it
really shines in JSX blocks (or other simple branches in literal data
structure definitions). It's also super helpful when needing to initialize
several variables based on branching on several other values. Having
expression-if combined with array unpacking assignment makes this a breeze.

------
acjohnson55
Oh yes! Besides pattern matching, this is the biggest improvement to
Javascript control flow I've been waiting for. It's a shame it can't be
expression oriented by default, but I suppose that in the handful of cases
expression-orientedness really shines, typing "do" is a small price to pay.

I have been wondering, it's there any downside to expression-orientedness?
Time and again, it seems to pay its weight. JS, for example, has arguably been
saved by function expressions.

Random niche question, would "do...while" now be an expression?

------
inglor
There was also talk of an asynchronous do notation proposal, and to add
monadic like functionality: [https://esdiscuss.org/topic/monadic-extension-to-
do-notation](https://esdiscuss.org/topic/monadic-extension-to-do-notation)

The problem is that these proposals haven't really advanced very much in the
spec process.

~~~
acjohnson55
I'm a fan of `async do` with `await`, rather than a poorly thought out
imitation of Haskell's `do`.

------
kaoD
I'm not sure I got the explanation right. Is this syntax sugar for
`(function(){ ... })()` ?

~~~
acjohnson55
More or less, with `return`s sprinkled in that desugaring.

Or you might say the syntax you quoted is a workaround for lack of expression-
orientedness.

