ECMAScript proposal: do expressions

关于 "do" 表达式的建议。「Proposal for `do` expressions」

Github stars Tracking Chart

ECMAScript proposal: do expressions

Status

This proposal is in stage 1 of the TC39 process.

Motivation

  • expression-oriented programming one of the great advances of FP
  • expressions plug together like legos, making more malleable programming experience in-the-small

Examples

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>
)

Tennant's Correspondence Principle

  • key refactoring principles:
    • do { <expr>; } equivalent to <expr>
    • (do { <stmt> };) equivalent to { <stmt> }
  • this semantic transparency is demonstrated by the semantics:
    1. Return the result of evaluating Body.

Further considerations

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.

Main metrics

Overview
Name With Ownertc39/proposal-do-expressions
Primary LanguageHTML
Program languageHTML (Language Count: 2)
Platform
License:MIT License
所有者活动
Created At2017-09-13 00:44:32
Pushed At2021-04-09 09:08:50
Last Commit At2021-03-02 17:53:12
Release Count0
用户参与
Stargazers Count1.2k
Watchers Count91
Fork Count14
Commits Count7
Has Issues Enabled
Issues Count74
Issue Open Count40
Pull Requests Count3
Pull Requests Open Count1
Pull Requests Close Count2
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private