Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do Expressions #155

Open
hzoo opened this issue Feb 15, 2017 · 10 comments
Open

Do Expressions #155

hzoo opened this issue Feb 15, 2017 · 10 comments

Comments

@hzoo
Copy link
Contributor

hzoo commented Feb 15, 2017

Babel docs: http://babeljs.io/docs/plugins/transform-do-expressions
Stage 1 gist reproduced below: https://gist.github.com/dherman/1c97dfb25179fa34a41b5fff040f9879 by @dherman

In Babel

interface DoExpression <: Expression {
  type: "DoExpression";
  body: BlockStatement
}

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>
)
@mikesherov
Copy link
Contributor

Seems fairly straightforward to me. @dherman any footguns to be aware of? @RReverser and @ariya any thoughts?

@ariya
Copy link
Contributor

ariya commented Feb 16, 2017

I haven't investigated do expressions yet.

BTW, why is the type Identifier?

@michaelficarra
Copy link
Member

One of the major controversies with the proposal within the committee is whether abrupt completions such as return/break/continue statements should be allowed within do expressions.

@RReverser
Copy link
Member

BTW, why is the type Identifier?

Seems like a copy-paste mistake to me.

@hzoo
Copy link
Contributor Author

hzoo commented Feb 16, 2017

Oops, fixed (good catch @ariya)

@gibson042
Copy link

type: "BlockStatement"

Don't you mean type: "DoExpression"?

@mikesherov
Copy link
Contributor

@gibson042, isn't the body of a do Expression just a normal block for the case of tree representation?

@gibson042
Copy link

Yes, subject to the concerns about completions @michaelficarra mentioned. But { "type: "BlockStatement", "body": […] } is an existing AST node, and it isn't an Expression.

@mikesherov
Copy link
Contributor

The body is a blockStatement (subject to abrupt completions), the type is "DoExpression"... no? what am I missing?

@RReverser
Copy link
Member

RReverser commented Feb 18, 2017

@mikesherov I think when @gibson042 wrote his previous comment

Don't you mean type: "DoExpression"?

main type of node was "BlockStatement", but @hzoo edited it afterwards, so it's now "DoExpression", thus misunderstanding :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants