Closed theScottyJam closed 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.
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.
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 .
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
anddo try
.do if example
do if
is similar toif
, except the if bodies are expressions instead of statements, and the else clause is required.do try example
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.