-
Notifications
You must be signed in to change notification settings - Fork 2k
Proposal: Optional (pre-last) function arguments #5318
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
Comments
Can you please include what the expected output JavaScript would be for your examples? Also are there any ECMAScript proposals at any stage of the TC39 approval pipeline that overlap with this? |
fn = (opt?, cb) -> do cb could compile into fn = function(opt, cb) {
if (typeof cb === "undefined") {
return [opt, cb] = [undefined, opt];
}
return cb();
}; fn = (opt ?= 42, cb) -> do cb could compile into fn = function(opt, cb) {
if (typeof cb === "undefined") {
return [opt, cb] = [42, opt];
}
return cb();
}; retriveRecords = (perPage ?= 20, page ?= 1, cb) -> could compile into retriveRecords = function(perPage, page, cb) {
if (typeof cb === "undefined" && typeof page === "undefined") {
return [perPage, page, cb] = [20, 1, perPage];
} else (typeof cb === "undefined") {
return [perPage, page, cb] = [perPage, 1, page];
}
};
I could not find anything related. |
So I think you would need to compile into This also looks challenging to implement. Keep in mind it would need to apply also for One more consideration is simply, how useful is this? Node uses the style in its callbacks of putting |
Here's a tidy general-purpose implementation: #try retriveRecords = (perPage, page, cb) ->
defaults = [20, 1]
[perPage, page, cb] = [
defaults[0 ... retriveRecords.length - arguments.length]...
arguments...
] The signature length would probably need to be hardcoded at compile-time, and for fat arrow functions the parameter list would need an CoffeeScript (and JS for that matter) typically has callback arguments as the last function parameter to allow inline function definitions at the call site. It's true that this creates the hassle of having to rearrange the arguments if some of them are optional. I too found this to be a common pattern/burden in my projects and am all for getting this language feature. I'm uncertain of the proposed |
It has been submitted before #1091, #4148 but it has been a while since last discussed so I'd like bring it forward again. I've been an avid coffeescript user since the early days and I missing this feature all the time.
instead of:
wouldn't it make it sense to have it as:
or better yet, decorating optional arguments with default values:
for example:
above function can be used neatly as:
or as:
my workaround at the moment is like this:
The text was updated successfully, but these errors were encountered: