Re: scope of variables
- Posted by jimcbrown (admin) Dec 30, 2010
- 2895 views
integer a = 10 procedure abc() integer a = 20 -- INVALID, a already exists in a containing scope end procedure
and the other change is to make all scopes work like this
integer a = 10 procedure abc() integer a = 20 ? a end procedure -- procedure output is 20
We have to think carefully about this. I'll ask my question again since it seems to be ignored, what is a valid use of redefining a variable in a more narrow scope? In the directly above synario you have now cut off all possibility of accessing the file wide a. Why would you want to do that? Bearing in mind that in a real application there would not be a file wide variable named a, it would be something useful such with a useful name.
For me it seems much more prudent that if there were to be a change the change to be never to allow a variable inside the same namespace to be redeclared. One may say it's heresy, but please tell me why and give valid examples as to why it is necessary to redefine a variable in a more narrow scope to mean something totally different than what it means in a file wide scope.
Jeremy
I'm going to go against the grain here. I'm fine with this breaking:
for .. integer b end for integer b
That should cause a redefinition error IMVHO. That'd make the behavior consistent and also keep things simple.
The only exception to this rule should be the one that was inherited from pre-4.0 - so locally defining a variable in a file and then redefining it inside a routine is ok.