GIve syntax error when using a local variable inside an assignment to… #2793
+31
−23
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
… that same local variable
Fixes #2142
I'm sending this as a PR because I'm not sure about this change.
Basically, this now gives a syntax error:
Previously that compiled fine: the
a
inside the assignment refers to thea
method. If no such method exists you would get an error saying "undefined local variable or method 'a'". Now it always gives an error.For this particular case, one can do:
Or
If inside a method and we want to refer to an instance method, we must use
self.name
now, as you can see in the diff in several places.The "downside" is that this also disallows declaring a variable with the same name inside an outer declaration:
Nor this:
But maybe this is good? It basically makes it impossible to write code that shadows a variable inside an assignment to that variable, which can be very confusing. And when wanting to call a method with that same it forces one to use
self.
, making it more explicit and obvious.Of course this is a breaking change.