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

syntax to allow optional parentheses for no-argument functions #1412

Closed
reissbaker opened this issue Jun 3, 2011 · 9 comments
Closed

syntax to allow optional parentheses for no-argument functions #1412

reissbaker opened this issue Jun 3, 2011 · 9 comments

Comments

@reissbaker
Copy link

I love that we can get rid of parentheses for functions with arguments -- but it feels a little inconsistent to write things like:
barista.prepare cup.coffee
...And then follow it up with:

`````` barista.frown()```

Ruby manages to have optional parentheses for everything by using special characters for instance variables (@, @@, $) to differentiate them from method calls. It'd be awkward/terrible to do the same in CoffeeScript for like a million reasons, but what about other syntactical ways to distinguish between function invocation and variable access? For example:
barista:prepare cup.coffee
barista:smile

Variables here are accessed via dot syntax; functions get called via colons. To access a function as a variable, you could write it in the regular way:
prepare = barista.prepare

The calling notation could work similarly for standalone function calls:
:prepare cup.coffee
:drink_all_cups

@michaelficarra
Copy link
Collaborator

You mean like do drink_all_cups?

@reissbaker
Copy link
Author

Similar, but not the same. For example:
:drink_all_cups:toString # colon syntax
do (do drink_all_cups).toString # current syntax
:drink_all_cups:toString:charAt 0 # colon syntax
(do (do drink_all_cups).toString).charAt 0 # ...lisp, is that you?

Basically, the colon syntax would allow for easy chaining without parentheses:
:drink_all_cups.someProperty:toString:toLowerCase:charAt 0
vs.
(do (do (do drink_all_cups).someProperty.toString).toLowerCase).charAt 0
This is basically what you can do in Ruby, except that Ruby's methods can't be called standalone, unlike Javascript's functions. Ruby's lambda keyword would get you there, though.

@michaelficarra
Copy link
Collaborator

Okay, re-opening for discussion.

@michaelficarra michaelficarra reopened this Jun 3, 2011
@arbales
Copy link

arbales commented Jul 16, 2011

hmm, interesting.

I like/love the ability to ditch the parens, but if consistency is important, I dunno if this accomplishes that.

foo(a, b, c)
foo a, b, c
bar b
:baz
do something

@sstephenson
Copy link
Contributor

#514

@arbales
Copy link

arbales commented Jul 16, 2011

@sstephenson void is more visually consistent, but I'd agree with @jashkenas that it's "shouty".

do has been useful for me in many cases, and perhaps the additional cue of parens in argument-less functions is a good visual indicator of its type, given that no comma-separated list follows.

@sstephenson
Copy link
Contributor

I agree with the conclusion to that ticket, too -- just wanted to point out the prior discussion.

@reissbaker
Copy link
Author

@arbales True, although your example could be rewritten this way:

:foo a, b, c
:bar b
:baz

etc. I guess my point here was more that you could write code with an internally consistent style if you wanted to, rather than enforcing a style across the entire language. If you want to keep using the parens, you could. The inconsistency of the current parens-less style is really the only thing that bothers me about Coffeescript at this point -- other than that, it feels perfect for my needs.

Right now it just feels like switching from curly-braces-on-the-right to curly-braces-below every third function call, depending on whether the function takes an argument.

@jashkenas
Copy link
Owner

Yep -- I think that this one is a re-hash of #514 (and more) with different syntax. The same arguments still apply in the same way. If you value consistency above being able to write:

print "Hello"

... please feel free to always use parentheses.

drinkAllCups().toString().charAt(0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants