coco icon indicating copy to clipboard operation
coco copied to clipboard

[Proposal] Optional function arguments

Open ceymard opened this issue 14 years ago • 5 comments

function myfunc (arg, [options], callback)
     ...

->

function myfunc (arg, _options, _callback) {
     if (typeof _callback === "undefined") {
          _callback = _options;
          _options = undefined;
     }
}

Or something like that...

This would greatly simplify declaration of functions with optional arguments that some framework use lately (like NodeJS)

ceymard avatar Oct 24 '11 12:10 ceymard

Related: https://github.com/jashkenas/coffee-script/issues/1091

Needed:

  • syntax
  • compilation strategy

satyr avatar Oct 24 '11 13:10 satyr

@ceymard Why not just void?

function myfunc (arg, [options], callback)
  void

thejh avatar Jan 08 '12 08:01 thejh

How does void solve my problem ?

ceymard avatar Jan 08 '12 13:01 ceymard

Uh, I misunderstood your proposal, sorry.

thejh avatar Jan 08 '12 14:01 thejh

Optional parameters without named arguments seems like it could get pretty hairy, no? Personally, I think any new dynamically-typed languages should require named parameters (after the first) - code is about reading, not writing.

mikeaustin avatar Jun 04 '12 22:06 mikeaustin