ECMAScript proposal: do expressions

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

Github星跟蹤圖

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.

主要指標

概覽
名稱與所有者tc39/proposal-do-expressions
主編程語言HTML
編程語言HTML (語言數: 2)
平台
許可證MIT License
所有者活动
創建於2017-09-13 00:44:32
推送於2021-04-09 09:08:50
最后一次提交2021-03-02 17:53:12
發布數0
用户参与
星數1.2k
關注者數91
派生數14
提交數7
已啟用問題?
問題數74
打開的問題數40
拉請求數3
打開的拉請求數1
關閉的拉請求數2
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?