Puzzled by this behavior
Timon Gehr
timon.gehr at gmx.ch
Wed Jun 1 00:14:37 UTC 2022
On 31.05.22 19:41, Don Allen wrote:
>
> 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
Not sure if it's necessarily "inconsistent", but it is quite annoying
and not really necessary. I have also complained about this before.
The simplest workaround is to make `foo` a template:
```d
import std.stdio;
int main(string[] args){
void foo()(){
bar();
}
void bar(){
writeln("Hello world");
}
foo();
return 0;
}
```
More information about the Digitalmars-d
mailing list