[Nickle]Scoping rules for declarations

Sergei Egorov nickle@keithp.com
Wed, 30 Jan 2002 12:37:14 -0500


Thanks for your answers. Let me summarize what I was
able to guess/figure out so far:

Local declarations are self-recursive and nested (as in C):
{ /* some local scope */
   mytype_t  foo = <foo_expression>;
   /* <foo_expression> can refer to (but not use value of) foo
       because it is in scope but not yet initialized;
       it cannot refer to bar below because it is not in scope yet
       (any reference to bar would refer to "outside" bar if any) */
   anothertype_t  bar = <bar_expression>;
   /* <bar_expression> can refer to (but not use value of) bar;
       it can refer to and use value of foo: it is in scope and initialized
*/
   <code> /* can use both foo and bar freely */
}

Comma-separated declarations are nested also:
{...
  mytype_t  foo = <foo_expression>, bar = <bar_expression>;
  ...
}
is equivalent to:
{...
  mytype_t  foo = <foo_expression>;
  mytype_t  bar = <bar_expression>;
  ...
}

Function declarations are sugared versions of variable declarations:
{...
  mytype_t  foo(<args>) {<body>}
  ...
}
is equivalent to:
{...
  mytype_t(<arg-types>)  foo = (mytype_t func(<args>){<body>});
  ...
}

Is this a correct picture of nickle local declaration rules? Is
it possible to "mix" declarations and statements inside blocks
(as in C99 and C++) or all declarations should come before the
first statement of the block (as in K&R C)?

What are the rules for file-level declarations? Can I make C-style
"forward" declarations if I am writing mutually recursive functions:

bool is_odd(int);
bool is_even(int n) { return n == 0 || is_odd(n-1); }
bool is_odd(int n) { return n > 0 && is_even(n-1); }

Regards,
Sergei