Postfix Block Expression
liamoc opened this issue · 6 comments
So, I'd really like something along the lines of ruby, that let you pass an additional argument to a function as a block specified after the function call, i.e:
myFunc(1,2) x =>
alert(x).
This would be semantically equivalent to either:
myFunc(1,2,x=>alert(x).)
or:
myFunc(1,2)(x=>alert(x).)
It should compile to the first case if we add it.
You prefer this style because the parenthesis doesn't need to wrap, right? It wouldn't be too hard to add to the grammar, but I think it could look a little funky on a single line, or for methods that don't take prior arguments. You're proposing:
list.each() x => x * x.
Versus the current compiler's:
list.each(x => x * x.)
Do you have any real examples where it helps make things clearer?
Partly i like it because it eliminates the need for specifying the end of a block twice. Typing ".)" is really awkward in my opinion, when I want to be able to define my own control structures.
Also, it drastically simplifies providing multi-line functions as parameters when you throw significant indentation into the mix.
It would be cool if CoffeeScript's lexer were smart enough to insert a period into the token stream for you, when you had an unclosed block within an outer parentheses. This may be do-able by counting parentheses. So:
list.each(x => x * x)
That would be nice, but significant indentation would also remove that problem.
My problem then is that I'm still specifying the end of the block twice - both with a paren and a dedent, or a paren and a period.. I'd rather get rid of the parens, but I guess it's just a pet peeve of mine.
Sorry, the block syntax is just too limited -- mentioned in the other issue. No multiple function arguments, and it has to be the last one. It wouldn't always be possible to call to with JS libraries with if they expected either. Closing as a "wontfix".