From guyd at cs.pdx.edu  Mon Nov 10 13:23:59 2003
From: guyd at cs.pdx.edu (David M Guy)
Date: Mon Nov 10 14:33:58 2003
Subject: [Nickle] Multiple definitions of variable names in same scope do
 not cause an error
Message-ID: <Pine.GSO.4.58.0311101318390.28639@sirius.cs.pdx.edu>


I wrote some nickle test code in which I made a copy/paste error,
accidentally declaring a variable name twice, but the interpreter did not
raise an error.

As an example, the following code parses and executes normally:

/**** BEGIN CODE SAMPLE  ****/
#!/pkgs/nickle/bin/nickle

int a = 3;
int a = 4;
printf("a = %d\n", a);  /* a = 4 */

exit(0);
/**** END CODE SAMPLE  ****/


Question: Is this a bug or by design?

Personally, I would prefer if the interpreter considered this an error.

Thanks,

David Guy

From bart at cs.pdx.edu  Mon Nov 10 14:44:26 2003
From: bart at cs.pdx.edu (Bart Massey)
Date: Mon Nov 10 14:44:33 2003
Subject: [Nickle] Multiple definitions of variable names in same scope do
	not cause an error 
In-Reply-To: Your message of "Mon, 10 Nov 2003 13:23:59 PST."
	<Pine.GSO.4.58.0311101318390.28639@sirius.cs.pdx.edu> 
Message-ID: <E1AJKlm-0001er-00@bart.cs.pdx.edu>

In message <Pine.GSO.4.58.0311101318390.28639@sirius.cs.pdx.edu> you wrote:
> I wrote some nickle test code in which I made a copy/paste error,
> accidentally declaring a variable name twice, but the interpreter did not
> raise an error.
> 
> Question: Is this a bug or by design?

By design. Each new definition of a name creates a new
variable, and they are all scoped properly.  So it is
normally harmless.

OTOH, it's a bug that the manual doesn't make this clear.

> Personally, I would prefer if the interpreter considered
> this an error.

Or perhaps a warning.  But keep in mind that there are some
useful idioms that would be borken if we did this, e.g.

	int i;
	for (i = 0; i < 10; i++)
	    if (test(i))
	        break;
	if (i < 10)
	    return i;

could not be repeated in a scope.

It's a tough design call, and one on which we've waffled in
the past.  We expect that most redefinitions are not errors,
but it would take an empirical study to figure it out
definitively.

Thanks much for the report!

	Bart

From guyd at cs.pdx.edu  Mon Nov 10 15:07:18 2003
From: guyd at cs.pdx.edu (David M Guy)
Date: Mon Nov 10 15:07:28 2003
Subject: [Nickle] Multiple definitions of variable names in same scope
	do not cause an error 
In-Reply-To: <E1AJKlm-0001er-00@bart.cs.pdx.edu>
References: <E1AJKlm-0001er-00@bart.cs.pdx.edu>
Message-ID: <Pine.GSO.4.58.0311101500060.20899@sirius.cs.pdx.edu>


But the following is a more common usage(imho):

/* BEGIN CODE */
int i;
for (i = 0; i < 10; i++)
    if (test(i))
        break;
if (i < 10)
    return i;
/* save 'i' in separate variable if necessary */
for (i = 0; i < 20; i++)
    if (test(i))
        break;
if (i < 20)
    return i;
/* END CODE */



And certainly this code does not suffer either way:

/* BEGIN CODE */
for (int i = 0; i < 10; i++)
    if (test(i))
        break;
/* END CODE */


Perhaps you could make a pragma, so the programmer could ask the
interpreter to enforce unique names? This would be greatly appreciated.

Thanks.
  - David Guy







On Mon, 10 Nov 2003, Bart Massey wrote:
> In message <Pine.GSO.4.58.0311101318390.28639@sirius.cs.pdx.edu> you wrote:
> > I wrote some nickle test code in which I made a copy/paste error,
> > accidentally declaring a variable name twice, but the interpreter did not
> > raise an error.
> >
> > Question: Is this a bug or by design?
>
> By design. Each new definition of a name creates a new
> variable, and they are all scoped properly.  So it is
> normally harmless.
>
> OTOH, it's a bug that the manual doesn't make this clear.
>
> > Personally, I would prefer if the interpreter considered
> > this an error.
>
> Or perhaps a warning.  But keep in mind that there are some
> useful idioms that would be borken if we did this, e.g.
>
> 	int i;
> 	for (i = 0; i < 10; i++)
> 	    if (test(i))
> 	        break;
> 	if (i < 10)
> 	    return i;
>
> could not be repeated in a scope.
>
> It's a tough design call, and one on which we've waffled in
> the past.  We expect that most redefinitions are not errors,
> but it would take an empirical study to figure it out
> definitively.
>
> Thanks much for the report!
>
> 	Bart

From guyd at cs.pdx.edu  Wed Nov 12 21:46:16 2003
From: guyd at cs.pdx.edu (David M Guy)
Date: Wed Nov 12 21:46:26 2003
Subject: [Nickle] Pragma to enforce unique variable names in each scope?
Message-ID: <Pine.GSO.4.58.0311122144500.10805@sirius.cs.pdx.edu>


Would it be possible to make a pragma, so the programmer could ask the
interpreter to enforce unique names? This would be greatly appreciated.

Thanks.
  - David Guy

From bart at cs.pdx.edu  Wed Nov 12 23:33:32 2003
From: bart at cs.pdx.edu (Bart Massey)
Date: Wed Nov 12 23:33:35 2003
Subject: [Nickle] Pragma to enforce unique variable names in each scope? 
In-Reply-To: Your message of "Wed, 12 Nov 2003 21:46:16 PST."
	<Pine.GSO.4.58.0311122144500.10805@sirius.cs.pdx.edu> 
Message-ID: <E1AKByu-0004hG-00@bart.cs.pdx.edu>


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?

	Bart

In message <Pine.GSO.4.58.0311122144500.10805@sirius.cs.pdx.edu> you wrote:
> 
> Would it be possible to make a pragma, so the programmer could ask the
> interpreter to enforce unique names? This would be greatly appreciated.
> 
> Thanks.
>   - David Guy
> 
> _______________________________________________
> Nickle mailing list
> Nickle@nickle.org
> http://nickle.org/mailman/listinfo/nickle

From keithp at keithp.com  Thu Nov 13 00:46:10 2003
From: keithp at keithp.com (Keith Packard)
Date: Thu Nov 13 00:46:14 2003
Subject: [Nickle] Pragma to enforce unique variable names in each scope? 
In-Reply-To: Your message of "Wed, 12 Nov 2003 23:33:32 PST."
	<E1AKByu-0004hG-00@bart.cs.pdx.edu> 
Message-ID: <E1AKD7C-0001L7-00@evo.keithp.com>


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:

	int i;
	for (i = 0; i < 10; i++)
		...
	if (i < 5)
		...

	int i;
	...

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.

-keith



From guyd at cs.pdx.edu  Thu Nov 13 12:28:46 2003
From: guyd at cs.pdx.edu (David M Guy)
Date: Thu Nov 13 12:28:56 2003
Subject: [Nickle] Pragma to enforce unique variable names in each scope?
In-Reply-To: <E1AKD7C-0001L7-00@evo.keithp.com>
References: <E1AKD7C-0001L7-00@evo.keithp.com>
Message-ID: <Pine.GSO.4.58.0311131212180.3354@sirius.cs.pdx.edu>


I agree that a special keyword for each variable name that you want to be
unique in each scope seems unwieldy, especially since an anal programmer
like myself would feel compelled to designate EVERY variable as 'unique'.

As for the 'i'-variable in this 'for' loop example, I think most
programmers (especially C-programmers) will habitually use the initializer
expression of the 'for' loop to reset the variable. If they don't, they
either forgot (not very likely) or they intended to use the previous value
as the initial value (far more likely).  I don't see how it could be a
good idea to redeclare your loop variable every time you use a 'for' loop
(unless you use the C++ version which allows you to declare a loop-scoped
variable in the initializer expression).

Are there other situations where this would be useful for other than
globally scoped variables?

 - David Guy



On Thu, 13 Nov 2003, Keith Packard wrote:
> However, there are legitimate uses for repeated definitions at local scope:
> 	int i;
> 	for (i = 0; i < 10; i++)
> 		...
> 	if (i < 5)
> 		...
> 	int i;
> 	...
>
> 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.

