Puzzled by this behavior

Don Allen donaldcallen at gmail.com
Tue May 31 17:41:18 UTC 2022


This

````
import std.stdio;

void foo() {
     bar();
}
void bar() {
     writeln("Hello world");
}
int main(string[] args)
{
     foo();
     return 0;
}
````

compiles and does what you'd expect, despite the forward 
reference from foo to bar, which, of course, would not work in C.

But
````
import std.stdio;

int main(string[] args)
{
     void foo() {
         bar();
     }
     void bar() {
         writeln("Hello world");
     }
     foo();
     return 0;
}
````
gives the error
````
(dmd-2.100.0)dca at pangloss.allen.net:/home/dca/Software/d_tests$ 
dmd test5.d
test5.d(6): Error: undefined identifier `bar`
````
This only works if you interchange the order of foo and bar, 
eliminating the
forward reference.

This strikes me as pretty inconsistent behavior, especially to 
someone who has written as much Scheme and Haskell as I have. 
I've also not found it documented, though I could have missed it 
(if someone could point me to where this is discussed, I'd 
appreciate it). My main purpose in sending this message is to 
understand whether this is a bug or a documented "feature". If 
the former, I will file a bug report.

/Don


More information about the Digitalmars-d mailing list