-
Notifications
You must be signed in to change notification settings - Fork 5
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
Syntax #2
Comments
@littledan and I had some hallway-track conversations about this. My thoughts were to add a gen function foo() {} // same as function* foo() {}
foo = gen () => {};
class Foo {
x = 1
// No more ASI hazard!
gen foo() {}
} When creating an async generator, the order would be |
There is an ASI hazard here foo = gen ()
=> {}; |
That's a syntax error currently, right? I don't understand how it would cause a new ASI hazard. |
I can imagine only two variants: foo = (*)(arg) => {}; foo = (*(arg)) => {}; second could be simplified for one / zero arguments as: foo1 = (*arg) => {};
foo0 = (*) => {}; |
Adding a new |
More radical solution is using different brackets like foo1 = [] => {}
foo2 = [[first, second]] => {} // with array destruction
foo3 = [a, b] => {} |
TBH, any of |
|
Either is fine with me.
Least surprising, but still super ugly. I (as a relatively experienced JS dev) still struggle with the order for A keyword would be less esoteric, and much easier to Google for. So extending generator support to arrow functions gives us a chance to tackle both with one proposal. 😃 |
I vote for keyword |
There is the answer in your question: as |
I vote for const myGen = async gen () => yield await sth()
// or
async gen function myGen () { yield await sth() } It I think it is more writeable readable than
Thoughts on oother programming languages
|
@rumkin |
@ljharb - I agree. I also think it is easier to type and thus less useful to abbreviate. Researches found that "Shorter identifier names take longer to comprehend" |
If a keyword is introduced for the arrow case, does it necessarily mean also introducing it for the ‘longhand’ case? |
For consistency, I would hope so. |
If we look for inspiration in python, we notice that they have no keyword to declare a generator - it is implied by |
@FranzSkuffka, I think this work isn't relevant for well-known language syntax. It's about new identifiers. Such identifiers are always located in brain's short memory and meaningful names help our brain to build an abstract model faster. |
Added |
How about |
That’s between a const keyword and a const binding identifier. The arrow function is the part in the initializer, after the equals sign. These expressions can appear in many places, not just in const declarations. |
Is there any ASI hazard to |
hi! why not |
Functions have gone from Hence it feels like |
IIUC that’s the only case, yeah. The following would parse as a MultiplicativeExpression with an invalid right hand side:
The asterisk would match MultiplicativeOperator and the parentheses would match CoverParenthesizedExpressionAndArrowParameterList. If the covered part refined successfully to ParenthesizedExpression (e.g. That said, I wouldn’t have called this a hazard: it would throw a SyntaxError up front. It’s not a ‘trap’ like
The extent of the ‘hazard’ is just that a semicolon is necessary. The absence of one poses no risk of producing code that evaluates at all, much less with a different meaning from what was intended. (AFAICT) |
Does the keyword option have to be a variant on the word When teaching them, the word "Generators" has caused a lot of confusion in how they relate to "Iterators". So I started referring to them as "Iterator Functions" and that seemed to help people understand their relationship. So I think But even better than that: "Iter" is already a well-established abbreviation for "Iterator" so the keyword iter function fn() {...}
async iter function fn() {...} |
I would think that making the keyword for defining generator functions |
Maybe, but as it stands right now the relationship isn't seen by many. Anecdotally, I've talked to developers who describe generators as "pausable functions" (not sure where this comes from) or "how async functions are implemented under the hood" (which I think comes from transpilers), and I've seen them use the iterator protocol directly with while loops and calling |
A generator is a pauseable (synchronous) function. It also produces an iterator (but isn’t one). They are (sadly) the dominant implementation detail for transpiler output of async/await; i think that’s indeed where that one comes from. given that the syntax is |
I think I agree with the gist of the previous comment, but would point out that a ‘generator function’ produces a generator and that a generator is a specific kind of iterator. The function itself isn’t an iterator or a generator, at least not as the spec defines those terms. |
It's not personal preference - the word you're looking for is "anecdotal". The way I know is because I've seen people use the abbreviation "async" for "asynchronous" quite frequently, predating I completely agree with you that |
First: Why is this threat discussion about star or not? (replacement of star) This should not be a discussion about Changing the star to something else, would be something that the There are already a syntax in the language for generator method (whether people like that syntax or not!) Syntax for a function is: (
Syntax for an arrow function are: (with some special case like single return etc..)
To follow the same syntax as a normal function and not introduce multiple language syntax for the same functional logic,
I have a hard time undestanding why this is even up for discussion ? (again changing the star is something I like to see, but it has nothing to do with getting |
Agreed, I previously posted a comment about that in #8 but this is a better place for it: ASI is the awful plague of Automatic Semicolon Insertion. The problem here is that ASI is only done if not inserting a semicolon before the next token would result in a syntax error, while in this situation
the parser will happily accept However, I think this ASI hazard can also just be ignored since there's no reason to ever put an arrow-function/generator expression at the start of an ExpressionStatement, since this is grammatically only permitted if the arrow body is followed by a comma or a semicolon and in either case the function is discarded. Note for comparison that it's not even possible for an ExpressionStatement to start with a non-arrow function/generator expression, since attempting to do so result in it being parsed as a function/generator declaration instead:
This despite the fact that putting a non-arrow function expression at the start of an ExpressionStatement would (unlike the arrow case) not be useless, since
BTW there's no grammatical ambiguity in Currently the only context where the grammar allows an ArrowFunction after a In conclusion, the ASI hazard seems like a complete non-issue to me. |
But you could make it iterable with |
Ew, that's so cursed. I'd sincerely hope there's noone out there who has actually done that, but I guess it would also be an option to just keep parsing |
if I « think outside the box » why can't we use =>> |
"the psychedelic syntax" :) |
Regarding
Personally, I feel the ASI hazard concern is a bit overblown for this. Edit 2: I notice after I send this that it's restating much of #2 (comment). This does add a few more things to round out the idea, though. |
One problem with all the *=> and *> etc is that they look really nice in code fonts where * is middle aligned but pretty weird in normal fonts. If you discount the idea that the syntax looks pretty because all the symbols line up perfectly, then *() => {} seems like the more natural syntax to me. |
It is ambiguous. |
Ehm, my statement was about arrows, not about non-arrow function expressions. The current grammar does not syntactically allow an arrow function expression as operand to the * operator:
|
This is already an old and long discussion but the goal stated in the beginning is still worth while. I'd like to refocus and offer my perspective. For me a generator arrow function is not about one-liners or easier to read semantic (though these are an added benefit for me) but the auto-binding of let that = this;
function * foo() {
that.nowWeHaveBinding
} And in most cases where I wish I had an arrow-function generator is in that situation when I want to make a generator in my closure but have the |
I'd like to add my $.02 and concur with @mvduin and @dead-claudia. In addition to the reasons they put forward, ubiquitous integration of linters, formatters, language servers, and particularly TypeScript makes the ASI hazard feel like it's receding from 2023 close to, if not past, the vanishing point. The fact is that |
Fixed. 🙂 The fact is that my personal intuition regards
In contrast, placing the generator asterisk next to the arguments list of an arrow function ( Ultimately (assuming this proposal eventually moves forward), syntax will be selected that will require one of us to adjust our intuitions. I'm simply pointing out that intuition can be subjective, and explaining how I (and others here) have reasonably arrived at different syntax suggestions.
On this point, we agree. 🙂 |
Except we also already have the |
@mvduin yes but that construct is understood to be purely a syntactic sugar for There's no need to sugar the arrow version like that because you'd just write |
Agree. All the strange gnobbles of the original That said, I do think that given the full breadth of how functions are expressed in JavaScript, the bit that marks the function variant appears very much in the head position. I think this is where a lot of the appeal of the
Which is not to say that it isn't more intuitive in the individual context of a expressing a generator lambda , but it does seem to me that by not following this convention, you are introducing not just another sequence to parse and assign meaning to (
Rather than "aligned with intuition", I think it is safer to say more narrowly that |
I still stand by my assessment that It also neatly follows the existing patterns with generators, so people would know it right away. |
@conartist6 it is not purely sugar; concise methods can't be constructed (they have no |
(minor nit: arrows can also use that syntax, but only lexically & depending on the outer env they close over. point remains of course!) |
I personally wouldn't say it is a bad thing here. The symbol is derived from an existing one, it looks like a variant of With Someone may say, that with async functions Another reason is the variant with one parameter:
In my perception, this kind of already happened with all arrow functions, where instead of the word In the end though, whichever syntax will be chosen, people will get used to it. |
It would be amazing to see some movement on this in one direction or the other. What would it take? |
This feels part and parcel of a category of asymmetries that leave generators in the lurch. I find myself wanting higher order iterators more and more. for yield* (let item of iterable) {} or the like |
@cowboyd You could just do nested |
I agree, it has little to do with this bug. I'm more pointing out that there are a lot of asymmetries where generators are like second class citizens compared to |
Instead of discussing introducing new syntax like What matters is, today, any of the following is understandable by someone who already knows that
It's as simple as combining the token that creates a function with Let's confirm that the final syntax will be one of these to limit the scale of the discussion Brendan Eich just reaffirmed his support for this proposal too, so there is no better time to get actual momentum 🤠🚀 Just to make sure, @hemanth, are you still championing it? |
I'm still not convinced the ASI hazard is a real issue with the If you do that, the ASI hazard goes away, no cover grammar needed. The only other hazard is Other than that, I don't see anything else that could be in the way of the syntax |
@gurgunday I agree whole heartedly that it is important to focus, and my vote would be the same as @dead-claudia:
I don't actually have a vote, but as someone who spends most days using JavaScript generators I do have an outsized interest. So yes, winnowing the discussion to one of the options you've outlined would be a great first step. So how does this decision to winnow get made? Assume it would take some action from @chicoxyzzy? |
Possible solutions
Arrow function syntax
Introduce new generator keyword for both function and arrow function
Previous discussions https://github.com/tc39/proposals/issues/216
The text was updated successfully, but these errors were encountered: