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

Optional curlies around objects as method parameters #30

Closed
jnicklas opened this issue Jan 3, 2010 · 4 comments
Closed

Optional curlies around objects as method parameters #30

jnicklas opened this issue Jan 3, 2010 · 4 comments

Comments

@jnicklas
Copy link

jnicklas commented Jan 3, 2010

I really like Ruby's way of making the curlies optional when passing a hash as the last argument to a function:

 monkey "blah", { :foo => 2, :bar => 3 }  # with curlies
 monkey "blah", :foo => 2, :bar => 3      # without curlies

It makes it look like named arguments. Is it possible to do the same in CoffeeScript?

monkey("blah", { foo: 2, bar:3 })        # with curlies
monkey("blah", foo: 2, bar:3)            # without curlies

Currently, this compiles to:

monkey("blah", {
  foo: 2,
  bar: 3
});
// with curlies
monkey("blah", foo = 2, bar = 3);
// without curlies

So the second example is valid Coffee, and compiles into an assignment. My suggestion is to make the second one compile to the same code as the first one.

Seems to me like this would be pretty hard on the parser and potentially introduce some confusion, so I'm not sure this is worth it at all, what do you think?

@jashkenas
Copy link
Owner

Right, that's the problem -- it's already valid CoffeeScript. Aspects of this have been addressed in:
http://github.com/jashkenas/coffee-script/issues/closed#issue/16
Which deals with default arguments.

The problem with having things look like named arguments is that they're not, really. Instead of getting them as optional local variables within the function, you have to look inside of the options hash. I think that this is an important distinction to have, especially because there's no reason why CoffeeScript couldn't support real named arguments (if the ambiguity with the assignment syntax can be worked out).

@weepy
Copy link

weepy commented Jan 3, 2010

TBH - I think it's best to avoid thing like this as it makes the model harder to understand. One of the nice things about Javascript is that there aren't too many rules about it's syntax.

@jnicklas
Copy link
Author

jnicklas commented Jan 3, 2010

You're both right, I'll close this issue. Missed the closed issue, sorry.

BTW: holy crap! splats! That's so awesome, CoffeeScript rocks!

@jashkenas
Copy link
Owner

It's all good. It's great to have this stuff on record, so people can read through the discussion. If you're looking to see what the limits of the syntax are, I'd recommend reading through the scripts in tests/fixtures/execution. That's the bulk of the current test suite -- each test should only compile lintlessly, and only ever print "true".

gregwebs referenced this issue in gregwebs/contracts.coffee Mar 30, 2012
alangpierce added a commit to alangpierce/coffeescript that referenced this issue Feb 12, 2018
This issue was closed.
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

3 participants