Let's say you could. Then you could do:
void main(string args[]) {
void a() {
b();
}
a();
int x = 42;
void b() {
// do something with x
a();
}
}
And viola - you use x
before declaring it.
There are workarounds to that - like ratchet freak said you could use a delegate, but you could also use a struct:
void main(){
int x=5;
struct S{
void a(){
writefln("in a, x=%d",x);
--x;
if(0<x){
b();
}
}
void b(){
writefln("in b, x=%d",x);
--x;
if(0<x){
a();
}
}
}
S().a();
}
Notice that both solutions prevent using x
before it's declaration. If you use a delegate you can't call it before you assign a function to it, which you can only do after you declare the other function, which happens after you declare x
. If you use a struct you can't declare x
either before of in the struct - but you can only call the functions after the struct is declared - which also means after x
is declared.