Forward referencing functions in D

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Oct 16 20:22:26 UTC 2020


On Fri, Oct 16, 2020 at 07:55:53PM +0000, wilcro via Digitalmars-d-learn wrote:
> The web page "Programming in D for C Programmers"
> (https://dlang.org/articles/ctod.html#forwardfunc) states that forward
> declarations are neither required nor permitted,
[...]
> However, the following code will cause a compiler error:
[...]
> void main()
> {
> 
>     void myfunc() {
> 
>         forwardfunc(); // onlineapp.d(8): Error: undefined identifier
> forwardfunc
>     }
> 
>     void forwardfunc() {
> 
>         writeln("foo");
>     }
[...]

This is because order-independence of declarations only applies to
module scope, not to function scope.  So the above would work if myfunc
and forwardfunc were moved outside of main().  But inside a function
body, you must declare everything before you use them.


T

-- 
Philosophy: how to make a career out of daydreaming.


More information about the Digitalmars-d-learn mailing list