-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Proposal: Feature Request/Suggestion: default argument using conditional assignment operator ?= #4999
Comments
It was discussed in #4945. I think it wasn’t implemented because if you allow it for regular arguments you should also support it for destructuring, and the destructuring case is too complicated to support in any reasonable way. In other words, I think where that thread concluded was that changing your (arg) ->
arg ?= true |
Migrating to CS2 would require adding It's disappointing that the very unambiguous use of |
The 1.x code for destructuring was very, very complicated. I don’t want to resurrect it just so that we have a way to do the 1.x version of default values, whether in function arguments or destructuring. It would be a significant burden (and bug-prone) to additionally maintain this 1.x style of destructuring going forward, even if it were sectioned off behind the And it would be necessary to support The ECMAScript committee made an annoying, some would argue incorrect, choice in making default values applied only when the target is undefined rather than falsy. This is discussed in great detail in #4945, but the consensus at the end was that CoffeeScript is just JavaScript, and if this is the way JavaScript went then we need to follow. We kept our current behavior for the existential operator because it has no equivalent in JavaScript (yet), but if they eventually adopt it too but use the same “undefined only” implementation, we’ll need to follow their lead. |
Current Behavior
As CS2 now compiles a default function argument directly to the ES6 syntax, this is one if the major compatibility breaks with CS1, since it strictly checks for undefined but not null.
Context
Surprisingly I have found that the default argument breaking change is the one which (for me at least) will require the most effort to upgrade from CS1 to CS2. When converting, of course it's possible to rewrite every function and put the following line at the top, however that also means adding some several hundreds of lines across multiple codebases.
Possible Solution
May I suggest allowing use of the conditional assignment operator to allow default arguments in CS2 to work the same way they do in CS1?
(EDIT) I did look for issues suggesting precisely this feature and found none, although it has been suggested multiple times in comments on other issues.
The text was updated successfully, but these errors were encountered: