-
Notifications
You must be signed in to change notification settings - Fork 2k
Disallow arguments with identical names #1002
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
I completely disagree. This is perfectly okay in JS and should be allowed in CS as well. For a use case, how would I cleanly skip arguments like this?
Even though it's not the most useful of features, I would be surprised if I was not allowed to do this. |
I'm not sure what's so clean about declaring a function with In fact, I can't find documentation of this "feature" of JavaScript anywhere online, so I can't be sure it's consistent with the standard, nor if the precedence is the same across platforms. If you want to put obfuscated garbage arguments in your function's signature, though, it would look cleaner if CoffeeScript translated |
|
So there you go. Michael, how about if we implement a syntax analogous to the one I've proposed at issue 870 so that you could write
and the generated argument list would be something like That way, you could more clearly indicate that the arguments are ignored, your generated JavaScript would be more valid/readable, and folks like me would still be able to get an error message at compile time when we accidentally type |
Yep, after satyr pointed out that this was not valid in strict mode, I'd (unfortunately) have to agree that this should throw an error. I want to keep as close to strict mode compliance with JS output as we can. An alternative solution would be to allow this and only name the last parameter with that name properly (renaming the others to some nonsense like Also, I'd like to reiterate that I am not a fan of the single-value-skipping |
It appears we throw an error on all strict mode requirements listed at satyr's link except this one and |
By the way, Michael, there is a (perhaps overly clever) way to write your example using pattern-matching:
Of course, that assumes that exactly 6 arguments are given. Once some kind of single-value-skipping syntax is implemented (issue 870), you'll be able to use that. Though |
|
Has anyone fixed this yet? |
@jamierumbelow: nope, that's why it's still open. Feel free to send a pull request. |
@michaelficarra -- Working on this (based on @jamierumbelow 's patch) :) |
Be very careful using a hash to check for the existence of names:
... either use |
I'm looking to build a consensus on this issue before working on my patch (#1531) again. We have three options for solving the issue of duplicate parameter names. (All involve ensuring the output JS has no functions with duplicate parameters.) Option
Option
Option
Philosophically Usability Consistency
If CoffeeScript also disallows that kind of code, which option from above would work best? For defining props in object literals, I can't see any reason why one would want placeholder/duplicate props -- is there one? If not, generating a unique name might not be a good solution if consistency is important. Personally, I like option |
My preferred option is
|
+1 for option #1 |
@TrevorBurnham There is a valid reason for
Callbacks with 2+ params that are not germane to the function's body can be more elegantly declared with placeholder params. |
@geraldalewis Yes, that code does look nice and readable. But more often, I'd think that people would give two arguments the same name by accident. In such cases, a compile-time error is very helpful. See my original post for this issue... This is the sort of problem that's best addressed by a programming convention, not special syntactic rules. I'm guessing that Jeremy's preferred approach to your code would be to say what the first two arguments to that callback are, even though they're unused. That's a good approach. Another would be to take those argument names and shorten them down to 1-2 characters. That'll keep your code succinct but still help you figure out what those arguments are in case you want to use them later. So style 1 (my attempt at emulating jashkenas) would give you
and style 2 would give you
I think either is preferable to adding yet another burden to poor |
@TrevorBurnham I agree, especially that constraining options through a strict interpretation can be really beneficial to usability. (Though I'm partially in favor of heaping more abuse upon |
You have Option 3 working already if this syntax is tolerable:
The compilation needs to improve, of course. |
@geraldalewis: I've been purposely withholding my opinion because I don't feel that any argument I can make in support of any of the options is strong enough. So, for now, I'll forfeit my vote. I only recommend that |
@TrevorBurnham's done a fine job of "guessing Jeremy's preferred approach". The more we debate it, the more it seems like
is a serious antipattern. For the reader who comes across your function, intending to modify it ... not only are they unable to use the first three parameters -- they also have not the slightest clue what the parameters might be. For a language that explicitly tries to discourage shadowing in favor of nicely named local variables, this seems like the ultimate in parameters shadowing, in a way. Every unused parameter in your program has the same name, and none of them can be used without changing it. Ugh. |
Moving this party over to #1547, for general use strictness. |
You can allow duplicate names, but only if the name isn't used in the function, i.e.: # perfectly fine
(_, _, _, buffer) -> buffer
# syntax error
(_, _, _, buffer) -> _ |
No, not even. A reader coming later would want to know what are these params in the first place if he needs to change the function. |
It's currently legal to have two function arguments with the same name:
Apparently the output is valid JavaScript; the result (at least under Node) is
2
. But I'd rather see an error at compile-time; code like this is almost certainly a typo. (For instance, an Express app written in canonical style will have several functions with the argument list(req, res)
—it's pretty easy to type(req, req)
or(res, res)
instead, with disastrous consequences at runtime.)The text was updated successfully, but these errors were encountered: