[Nickle] Pragma to enforce unique variable names in each scope?
keithp at keithp.com
Thu Nov 13 00:46:10 PST 2003
Around 23 o'clock on Nov 12, Bart Massey wrote:
> In general, we aren't up for "pragmas"---metasyntax is bad.
> OTOH, I can imagine a scope keyword that would cause a decl
> to fail if it was scoping out something, ala
> unique int x = 0;
> or somesuch. Keithp? What do you think?
The main reason that definition creates new variables without error is so
that you can reload files full of code and replace all of the existing
definitions of those variables with new ones. That, of course, is only
relevant at global scope.
However, there are legitimate uses for repeated definitions at local scope:
for (i = 0; i < 10; i++)
if (i < 5)
The goal here is to permit the redefinition so that you don't reuse 'i' in
a similar but not identical fashion (like indexing two different arrays).
By allowing the redefinition, we assert that each 'i' is actually
separate, and unrelated to any 'i' above in the program.
I know this isn't consistent with C which requires all declarations to
occur at the begining of the function, but I think it's less error prone
and makes maintaining the code easier as declarations and code are
held together in a single block.
I don't see the practical utility of requiring a new keyword for every
declaration to change this behaviour though; it's just noise most of the
time, obscuring the real program.
More information about the Nickle