tc39 / proposal-do-expressions

Proposal for `do` expressions
MIT License
1.12k stars 14 forks source link

Create expression-oriented control structures instead #69

Closed theScottyJam closed 3 years ago

theScottyJam commented 3 years ago

The motivating purpose of this proposal, as explained in the README, is to allow Javascript to be used in a more expression-oriented fashion.

Right now this is accomplished by creating a do block, and interpreting the last statement in the block as an expression. Because not all statements make sense as an expression, we're black-listing a bunch of statements, like if without else, loops, etc.

What if, we instead made Javascript more expression-oriented by simply providing control structures that are expression-oriented (similar to how other expression-oriented languages solve this problem). This would allow us to use control structures as expressions without having to first wrap them in a do block (making them less verbose to use). Turns out, to achieve this objective we would only need to add a couple of new control structures: do if and do try.

do if example

const val =
  do if (count === 0) "none"
  else if (count === 1) "one"
  else if (count === 2) "a couple";
  else if (count <= 5) "a few"
  else "many"

do if is similar to if, except the if bodies are expressions instead of statements, and the else clause is required.

do try example

const result =
  do try (
    readFile('./myFile.txt')
  ) catch (err) (
    err instanceof FileNotFoundException
      ? null
      : throw err // I'm relying on the "make throw an expression" proposal here
  )

do try is similar to try-catch, except the bodies are expressions, and the finally clause is not allowed.

Conclusion

The three main use cases for do blocks turning statements into expressions seems to be for "if", "try", and "switch". Two of these three would be solved by the above examples, and "switch" will be superseded by "match" (which is an expression) in the pattern-matching proposal.

Many (including me) have gravitated to this do-expression proposal because of another use case that isn't the "motivation" for this proposal - providing a mini scope to create and use temporary variables. This, however, can be achieved through other means, like providing a better, less hard-to-read, syntax for IIFEs. mini-scopes and control structures as expressions are unrelated issues, and combining two solutions into one feature is making that feature subpar at handling both problems.

ljharb commented 3 years ago

You can already use curly braces to create and use temporary variables; what do expressions give you (among other things ofc) is the ability to do that in expression position without an IIFE.

bakkot commented 3 years ago

This seems like a duplicate of https://github.com/tc39/proposal-do-expressions/issues/39. In the interest of keeping discussion consolidated I'm closing this issue in favor of that one. Feel free to repost this as a comment there.

01taylop commented 3 years ago

Nice

On Fri, 4 Jun 2021 at 19:13, Kevin Gibbons @.***> wrote:

Closed #69 https://github.com/tc39/proposal-do-expressions/issues/69.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tc39/proposal-do-expressions/issues/69#event-4844615378, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFRSQAGVYB76AT43KWJZMTTREJURANCNFSM46DH635A .