[Nickle]Scoping rules for declarations

Keith Packard nickle@keithp.com
Thu, 31 Jan 2002 07:13:43 -0800


Around 0 o'clock on Jan 31, Jamey Sharp wrote:

> > real function baz (int x);
> > real function bar () { return baz(37); }
> > int function baz (int x) { return x + 5; }
> > bar()
> 42
> 
> Except I don't know nickle well enough to check the return type of baz.
> Will there be a type promotion in either of these functions? I'm
> curious.

The requirement is that the definition of baz be a subtype of the 
declaration; otherwise typechecking in the interim will not be statically 
sound.  In this case, 'int (int x)' is a subtype of 'real (int x)'.

Nickle doesn't have any promotions, only subtypes -- int is a subtype of 
real, so any function returning real may return an int as well.  Nickle 
has a few functions that coerce values, like 'floor' converts real to int.

Because of the lack of promotions, it is possible to have a program which 
is dynamically type correct but not statically type correct:

	int x = 5;
	real y;
	int z;

	y = x;
	z = y;		/* fails static typechecking */

	poly w;

	w = y;
	z = w;		/* succeeds run-time typechecking */

If Nickle used promotions instead of subtyping, this example would fail
at run-time.

Keith Packard        XFree86 Core Team        Compaq Cambridge Research Lab