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

Big changes to generator/async fn/comprehension syntax #92

Open
Nathan-Wall opened this issue Sep 5, 2014 · 0 comments
Open

Big changes to generator/async fn/comprehension syntax #92

Nathan-Wall opened this issue Sep 5, 2014 · 0 comments
Labels

Comments

@Nathan-Wall
Copy link
Owner

Background

There are two forms of generator and async syntax that could be helpful to have:

  1. Define a generator or async function
  2. Run a generator or async block of code

Proto has taken an ES-style approach to solving the second type with either IIFEs or comprehensions.

// Type 1
async foo() :{
    var value = await bar(3);
    // ...
}

// Type 2 with IIFE
(async :{
    var value = await bar(3);
    // ...
})();

// Type 2 with comprehension
{:
    var value = await bar(3);
    // ...
};

Proposal

Add a dedicated syntactic form for type 2 that's more prominent than the existing comprehension syntax.

Impetus

I expect type 2 to be pretty common, and the IIFE is ugly while the comprehension is too easy to miss when skimming code (looks like a regular block unless you really look closely). We can do better.

Pros

  • Clean, readable syntax
  • Can drop comprehensions (they're already closer to type 2 than ES or Python comprehensions).
  • Extensible. We can easily add new forms without having to make syntax crazier and crazier. Just use a keyword like gen, async, or array to specify the kind of block. We could potentially want to add more kinds of blocks. Maybe block types could even be user-defined?

Syntax Options

The form async :{ } will probably be used for either type 1 or type 2. Both seem to be reasonable options.

Favoring Type 1

// Type 1 (function)
async foo(bar) :{
    // ...
}

// Type 2 (block)
do async :{
    // ...
}
Pros
  • Harder for someone to accidentally use a block instead of a function. If we use the option below, I can see people typing var foo = async :{ }; and expecting foo to be a function when really (under the option below) it would be a promise, just because they forgot fn. This could be an annoyance. If the option above is chosen, I think it's less likely that people will forget the do. I'm not sure why, but it just seems that way...
Cons
  • This option would look really weird for array blocks:
var x = do array :{
    for i of 0 .. 10:
        each i ^ 2;
}

Favoring Type 2

// Type 1 (function)
async fn foo(bar) :{
    // ...
}

// Type 2 (block)
async :{
}
Pros
  • This option looks much better for array blocks.
var x = array :{
    for i of 0 .. 10:
        each i ^ 2;
}
Cons
  • See the pros for the first syntax option. It seems too easy for someone to forget fn when they intend to write a function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant