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

Argument Enhancements (Defaults and Splats) #16

Closed
jashkenas opened this issue Dec 28, 2009 · 9 comments
Closed

Argument Enhancements (Defaults and Splats) #16

jashkenas opened this issue Dec 28, 2009 · 9 comments

Comments

@jashkenas
Copy link
Owner

Think about adding argument defaults to CoffeeScript function definitions. Something along the lines of:

start_reading: book, page: 1 => ...

Ideally, the arguments with default values could occur anywhere in the list.

Also, splats to siphon up the rest of the arguments:

demolition: *condominiums => wreck(condo) for condo in condominiums..

Neither of these should be too hard to implement -- both can be accomplished by inserting an expression that does the work at the top of the Code body.

@weepy
Copy link

weepy commented Dec 28, 2009

Like it alot.

Could also have ruby 2.0 style named arguments? E.g

start_reading(book: "The Jungle Book", page: 0)

Might be more complexity than you're willing to add at this point.

@henrikh
Copy link

henrikh commented Dec 29, 2009

I like both ideas. Weepy's can be implemented using objects (The "normal" way in plain Javascript).

@jashkenas
Copy link
Owner Author

I like weepy's idea too, -- it's a nice shorthand in Ruby, and it would be nice to have it here. The only twist is that it's already valid CoffeeScript syntax.

start_reading(book: 'The Jungle Book', page: 0)

Compiles into:

var book, page;
start_reading(book = 'The Jungle Book', page = 0);

So we might need to keep the brackets wrapper just to be specific about what you mean.

@weepy
Copy link

weepy commented Dec 29, 2009

how does ruby manage it ?

@jashkenas
Copy link
Owner Author

Well, in Ruby, assignment uses =, not :, so it's not ambiguous. I don't really mind the brackets though -- it's nice to know that you're passing an arguments object and not named arguments -- they don't work the same way within the method.

@jashkenas
Copy link
Owner Author

Alright -- splats in function definitions are now on master. Kick the tires if you'd like. This:

party: host, *guests =>
  guests.each(guest => host.danceWith(guest))

Compiles into:

var party;
party = function party(host) {
  var guests;
  guests = Array.prototype.slice.call(arguments, 1);
  return guests.each(function(guest) {
    return host.danceWith(guest);
  });
};

@jashkenas
Copy link
Owner Author

And splats are now available as arguments to function calls. Calling the function defined above:

party(me, *friends, *relatives)

Compiles into:

party.apply(this, [me].concat(friends).concat(relatives));

@jashkenas
Copy link
Owner Author

Going to leave default parameters out for the time being, because they would significantly complicate the grammar and have the potential to look really confusing within function assignments. If anyone has a proposal for what default parameters should look like, feel free to open a new ticket. Closing this one.

(If you'd like to have default arguments now, you can use the ||= operator.)

@josher19
Copy link
Contributor

New ticket: http://github.com/jashkenas/coffee-script/issues#issue/92
discussing default parameters.

Problem with using ||= is that it is bug-prone.

Horse: name, speed => 
    this.name: name
    this.speed: speed || 45

deadHorse = new Horse("Gluey", 0);
"This dead horse has a speed of " + deadHorse.speed  + "!"
 # ... 45!

alangpierce pushed a commit to alangpierce/coffeescript that referenced this issue Jan 2, 2017
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

4 participants