Skip to content

Instantly share code, notes, and snippets.

@dherman
Created June 18, 2012 02:53
Show Gist options
  • Save dherman/2946596 to your computer and use it in GitHub Desktop.
Save dherman/2946596 to your computer and use it in GitHub Desktop.
// This isn't a proposal for JS. It's just a little thought experiment:
//
// What if I could go back in time and redesign the semicolon insertion rules?
//
// One of the most annoying things about ASI is when it *doesn't* kick in because
// the following line starts with ( or [. The Go language has chosen a different
// policy, inserting semicolons even when the following line starts with ( or [.
// So in JS, when you see:
f()
(g())
(h())
// it's parsed the same as:
f()(g())(h());
// Whereas in Go, it's parsed the same as:
f();
(g());
(h());
// I like Go's policy, but it does mean that if you do want to do multi-line call
// chains, you will get in trouble. So here's a thought: start with the Go policy
// and add an optional explicit "function call" operator:
f() • (g()); // equivalent to:
f()(g());
// This way, you could do multi-line function call chains with the explicit operator:
f()
• (g())
• (h());
// same as:
f()
• g()
• h();
// same as:
f() • g() • h();
// same as:
f()(g())(h());
// This is almost like Haskell's $ operator, except I've assumed it would be
// left-associative here. TBH, I'm not really sure what the best associativity
// would be.
// Anyway, this style of function call chaining is rare in JS (compared to, say,
// method call chaining), but you do occasionally see it, e.g. http://fabjs.org.
// I wonder what that would look like done with the explicit • operator for
// multi-line operations. Maybe horrible!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment