[Nickle]Scoping rules for declarations

Keith Packard nickle@keithp.com
Wed, 30 Jan 2002 23:32:58 -0800


After a brief deliberation, I've implemented forward declarations of 
functions.  This makes the 'function' statement more than just sugar at 
this point, but it's consistent with C, making it less objectionable than
some alternatives.

A function declaration is of the form

	<type> function <name> (args);

This creates a forward declaration of the name -- the scope of the name is 
covered by this declaration, not any succeeding definition.

A function definition is of the form

	<type> function <name> (args) { statements }

If <name> currently references a forward declaration of a function, then
this statement elides the declaration part of the syntactic sugar and 
leaves only the assignment part.  A type mismatch is an error (as it would 
be in assignment), although the function type in the definition may be a
subtype of the original declaration type.  This permits

	int function foo (int x);

	int function foo (real x) { return floor (x); }

This makes sense if you think of this as just an assignment statement
with different syntax.  Of course, in this context the type of 'foo' 
remains 'int (int x)' and so you can't call foo with a real number.
Assign foo to a poly variable and you can though:

	> poly bar = foo;
	> bar (7.2)
	7

-keith