-
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
void
keyword (or dot, or bang, for no-arguments function calls)
#514
Comments
Worth noting that +1 for this idea! |
My initial reactions to the idea were negative:
But, then I looked at some of my parenthesis-cluttered code, and asked myself which is easier on the eyes:
or
The latter is more succinct, but it takes a few seconds to figure out where the parentheses are used for breaking up the logic and where they're used for function calls. Now, this problem could be solved with rainbow parentheses, but TextMate doesn't support that (to my knowledge). So, a hesitant +1. I don't think it should be the canonical syntax for calling a function without arguments, but I think it should be a valid alternative in case of parenthetical soup. Update: sethaurus' comment above actually raises another concern. While Here's an idea: What about using This looks perfect to me:
|
TrevorBurnham: This isn't directly related, but if you feel like there's already too much more-than-one-way-to-do-it syntax, it would be good to have a ticket that proposes getting rid of some of it. I, for example, would be happy to use a CoffeeScript that only supported |
For the record, we actually don't support the Can anyone think of an example where the traditional |
Ah, should've checked that—I think it's a good thing for CoffeeScript to not support the I'd like to repropose the
The last one looks pretty amazing to me. It's both the shortest and the clearest. At first I was concerned about In fact, I think that |
or you could have |
What do folks think about weepy's suggestion? It's not currently valid CoffeeScript syntax, and I think it's an interesting choice. I tried converting a sketch to use it, and it makes it very clear which banged-functions are called only for their side effects, and which functions are actually called for their value. My main argument against it is that it makes CoffeeScript syntax more SHOUTY!, when I'm not sure that the mere fact of being side-effect-ful deserved all the connotations of an exclamation point... |
I like both TrevorBurham's and weepy's suggestions, and would be happy with either. The exclamation mark does come across as a little shouty, whereas the dot after the function name kind of looks like a typo at first glance, or that you are passing a single special parameter. But those are both minor points and I think both syntaxes are nicer than the current empty brackets. Not a big fan of the |
I'm going to come down hard against using the bang. It's just asking to be abused, since really it should only be used for functions with side effects. For example:
Will be all over the place because it shaves off two characters, but breaks the universal contract for what bang should be used for. Really, no matter how I slice it, I feel () should stay. There's not a pressing need to introduce new syntax for this particular case. The current syntax is clear, intentional, and short. The only real counterargument would be if there was a way to remove the need for ()'s altogether, but there's not, and arguably this would be a bad idea anyway since Javascript (being a functional language) is all about function application, and hence function calls should be explicit. |
I don't like the bang syntax. As a JavaScripter, I think that support for
I find the third one to be by far the most readable; it's the "calmest," with very little punctuation. |
javascript is not ruby and so i think we should allow somewhat free reign on the syntax without worrying about how other languages behave. I think the ! works, but we should certainly retain () support. I'm not a fan of the . as it looks like a property access |
I think gfodor's argument is persuasive. Closing the ticket as a |
Just a funny discovery - you can use backtics (embedded js) to call a function without arguments, without fn `` # backticks It allows for (nice?) chaining: fn = (args...) -> args.length
fn ``
.toString ``
.repeat 5 Above will give you nice fn().toString().repeat(5); |
I'd like to open discussion for a
void
keyword, such thatcompiles to
The idea came up in an IRC conversation about the dissonance of optional parentheses on function calls in cases like this:
In general, CoffeeScript style is to omit parentheses where possible. Omission is not possible for argument-less function calls. A
void
keyword could mitigate that:I'm not convinced it's an improvement over
@pass()
. But maybe there's something here. Quoth jashkenas: "it's an interesting idea to have a keyword that has a presence like a variable in the language, but disappears before use."The text was updated successfully, but these errors were encountered: