-
Notifications
You must be signed in to change notification settings - Fork 2k
Compound assignment operators (and ++/--) do not create scope #1122
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
Why would you need a
Doing any of the above on a non-existing variable should produce an exception. |
I agree with StanAngeloff. Only assignments should declare a variable. And I hope you mean a runtime |
I see what you're saying, but it feels weird that
Surely we can all agree that it's undesirable for there to be different output when one replaces Perhaps consistency could be better found by preventing expressions of the form |
Coffee's |
OK, I believe I spoke too early. Just today I had to fix an issue where I still think it was my mistake to use
It's crappy coding, which is not the point. |
Right; and I remember an issue on list comprehensions where it was said that
is the best way to get the first non-null value out of an array. That's great, unless there's a global called
Can we tag this as a bug now? |
Yes, in both your and StanAngeloff's code examples, the variables should be initialized first anyway. If you don't do that, you're asking to get screwed either by a global or a modification later on. |
Michael, I don't understand where you're coming from. It seems really, really weird to me that changing Are you really arguing that |
TrevorBurnham: It seems to me that you're suggesting I can never use closures to modify a closed-over value. Take a look at a CS variation of my proposed _.once implementation.
Should this make a edit: This may be made a little more clear by the following code:
Changing |
Well, given the discussion at #1121, it looks like the consensus is that the outer declaration of If so, then surely that argument would apply more generally, and scope should be made order-neutral as I suggested at #1121? Why should Again, an alternative proposal would be to have |
Well, it's not a property of the global scope. It's just an in-scope value that needs modification and not redeclaration. edit: I think the bug is derived from treating in-scope values as out-of-scope values when their initial assignment comes after their reference. Crap, that sounds really confusing. Here's an example, I guess:
and oddly
So pretty much our variable declaration hoisting rules are not respected by whatever is determining if a variable is in scope. |
Right, obviously
Should |
I agree, there's potentially multiple issues at play here. If you haven't already, see my edit above. |
|
It was a bug. Declaring with compound assignment means accessing the variable before initialization, which doesn't make sense. I made such assignments illegal later in Coco, but the change didn't get ported back. |
That's debatable; allowing "Fixing" |
I'm afraid that the two expressions should not be entirely interchangeable -- all compound assignment and compound operators operate under the assumption that their operands already exist in the current scope ... and should error if undeclared.
|
Jeremy, I urge you to reconsider this one. There are two core tenets of the "Zen of CoffeeScript" that I've heard you espouse many times that seem to be at stake here:
I get what you're saying about the meaning of compound operators, but that choice comes at no small cost in language complexity. |
+1 for reconsidering this issue. It took me some time to resolve a nasty bug because of unintentional creation of global variable. My code in essence looked like ->
for num in [1..10]
errors ?= {}
errors While I thought It will be great to see coffeescript eliminate any unintentional global variable creations other than the official way - |
@bhavinkamani: See pull #1729 and issue #1627. We plan to do exactly what you request. |
Thanks a lot @michaelficarra and everyone for great community effort on this... |
@jeremyBanks' patch to add early errors for compound assignments to undeclared variables is now merged to master. |
What about non-conditional compound assignments?
|
I'd tend to think that those are acceptable because they don't introduce global variables by accident, and will fail at runtime if invalid. We could be stricter and forbid them without |
Sounds like this issue should be wontfix rather than duplicate. |
Surely there ought to be a
var x
in there somewhere? Likewise withx += 1
.The text was updated successfully, but these errors were encountered: