-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add postfix return statements #2729
Conversation
They work like this: readFiles = (directory, cb) -> fs.readdir directory, (err, files) -> cb err and return if err? output = {} for file in files then do (file) -> fs.readFile path.join(directory, file), 'utf8', (err, data) -> cb err and return if err? output[file] = data files.pop() cb undefined, output if files.length is 0 `and return`, appended to a statement, parses to a POST_RETURN token, which triggers a special case on the Return node. The token is generated through the rewriter, by watching for and tagging any combination of `LOGIC/&&` followed by `RETURN/return`.
Take the start column and line of the `and` token and move them over to the resulting single `POST_RETURN` token.
While the purpose is noble, i don't like the usage of validateMove() and makeMove() This proposal would make the right-hand side of the Maybe another keyword could be used. This is also a compiler error as of now an also reads quite nicely: cb err then return if err? I personally don't see that much value in adding this... but we shall see. |
Agreed that I don't see value in this. However I would prefer |
@epidemian You're right! |
I don't like overloading the |
What about dropping the "linking" keyword entirely, then, and having "return" as a simple postfix operator like cb err return if err? |
A higher-order function can provide much of the benefit of the proposed syntax addition: handleError = (errorHandler) -> (success) -> (err, rest...) ->
if err then errorHandler err else success rest... With $$ = handleError callback
Fodo.roots $$ (roots) ->
root = _.find roots, predicate
Fodo.children [root], $$ (children) ->
# ... Without: Fodo.roots (err, roots) ->
if err
callback err
return
root = _.find roots, predicate
Fodo.children [root], (err, children) ->
if err
callback err
return
# ... |
Yep -- I don't think we should expand the meaning of
In an unrelated thought -- is there any good reason why |
Is there any distinction between "statement expression" and "value expression"? I could see a statement like |
There are a bunch of different ways to tunnel errors through asynchronous functions.
I propose adding a postfix return statement - that is, adding
then return
to the end of an expression would execute the expression and then return nothing.So, this:
Would compile to this:
This doesn't break any existing functionality, as
<expression> and return
is already a parse error.A more complex example:
In the attached implementation (which includes tests),
and return
, appended to a statement, is rewritten to aPOST_RETURN
token (the rewriter watches for aLOGIC/&&
token followed by aRETURN/return
token). This then triggers a special case on theReturn
node.Thoughts?