Variable scoping in Spin
David Betz
Posts: 14,516
I've been working on code for the C3 lately and have ended up using bits of code from various places and am frequently faced with the problem of having to rename function parameters or local variables because they collide with the names of globals or functions in some other part of the code. Is there any reason that Spin doesn't obey the normal variable scoping rules of any other modern language? Parameter names and local variable names should have a different scope than global names. Not only does the Parallax Propeller Tool impose this restriction on same named variables in different scopes but it seems BST does the same. I'm not sure about Homespun because I haven't used it much yet. Is there any chance the Spin compilers could adopt modern scoping rules for variable names?
Comments
I think you only have to get used to SPIN. The idea is to put toghether code/variables in one SPIN-file that belong together and NOT do the whole project in one BIG SPIN-file.
There is little to no chance that your wish comes true. Remember, the software is for free. And my wish is that PARALLAX concentrates their manpower on Prop II development ;o)
What's the problem in giving different memory locations different names? Say gfoo and lfoo in your case?
For me the killer argument against your wish is that it's not a "must have" because something important is not working - it's only an "I want to have".
You're correct. It doesn't make sense that you can't reuse global names as local variable (or parameter) names. I don't know why Chip did variable scoping this way. On the other hand, you have to work with the tools you're given (or make your own). I could imagine BST and HomeSpun having options to allow this, but unfortunately the default would have to mirror what the Prop Tool does.
This isn't really a big issue for me. I only bring it up because I've spend most of my life designing and building languages and these minor problems tend to bother me more than they ought to.
John Abshier
-Phil