[Nickle]Scoping rules for declarations

Keith Packard nickle@keithp.com
Tue, 29 Jan 2002 18:19:03 -0800


Around 19 o'clock on Jan 29, "Sergei Egorov" wrote:

> What are the scoping rules for nickle declarations: how can I
> get simultaneous, nested, and mutually recursive declarations?
> The man page is not very clear on this.

Bart showed a reasonable example for functions, except that I would have
done:

	int function g (int y);

	int function f (int x) {
		if (x > 0)
			return g (x-1);
		return 0;
	}

	g = (int func (int y) { return 1 + f(y); });

In an older definition of the language, redefinition of global variables 
used to share the same storage, in that case you could have done:

	int function g (int y);

	int function f (int x) {
		if (x > 0)
			return g (x-1);
		return 0;
	}

	int function g (int y) {
		return 1 + f(y);
	}

which looks a bit nicer on the page.  Semantically, this was something 
of a mess though -- redefinitions had to have "compatible" types, and 
these rules only applied at the global level as within a function it 
doesn't make any sense.  

Instead, a nicer looking version that doesn't require the above semantic 
wart uses a nested function:

	int function f (int x)
	{
	    int function g (int y)
	    {
	        return 1 + f (y);
	    }
	    if (x > 0)
	        return g (x - 1);
	    return 0;
	}

Now, if what you're looking for is recursive *datatypes* instead of 
recursive functions, that's really easy -- typedefs needn't be bound when 
used in anoter typedef:

	typedef	foo;

	typedef struct {
	    *foo  x;
	} bar;

	typedef struct {
	    *bar  y;
	} foo;

-keith