Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 16, 2020 Forward referencing functions in D | ||||
---|---|---|---|---|
| ||||
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, and that the following construct is allowable: void myfunc() { forwardfunc(); } void forwardfunc() { ... //do stuff } However, the following code will cause a compiler error: import std.stdio: writeln; void main() { void myfunc() { forwardfunc(); // onlineapp.d(8): Error: undefined identifier forwardfunc } void forwardfunc() { writeln("foo"); } myfunc(); } Evidently, I am misunderstanding something very elemental here; thanks for any enlightenment regarding this. |
October 16, 2020 Re: Forward referencing functions in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to wilcro | On Friday, 16 October 2020 at 19:55:53 UTC, wilcro wrote:
> Evidently, I am misunderstanding something very elemental here; thanks for any enlightenment regarding this.
Inside a function things happen in order, top to bottom, including declarations (you can only access local variables after they are declared, and nested functions work like local variables).
In a declaration, order is less important.
For recursive nested functions it can sometimes help to put a declaration inside a function:
void main() {
// order matters here, in function
struct Holder {
// order doesn't matter in here, in decl
}
// order matters again since functions run in sequence
}
|
October 16, 2020 Re: Forward referencing functions in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to wilcro | On Friday, 16 October 2020 at 19:55:53 UTC, wilcro 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, and that the following construct is allowable:
>
> void myfunc()
> {
> forwardfunc();
> }
>
> void forwardfunc()
> {
> ... //do stuff
> }
>
>
> However, the following code will cause a compiler error:
>
> import std.stdio: writeln;
>
> void main()
> {
>
> void myfunc() {
>
> forwardfunc(); // onlineapp.d(8): Error: undefined identifier forwardfunc
> }
>
> void forwardfunc() {
>
> writeln("foo");
> }
>
> myfunc();
>
> }
>
>
> Evidently, I am misunderstanding something very elemental here; thanks for any enlightenment regarding this.
I think it might be just because you havent defined the function yet at that point.
|
October 16, 2020 Re: Forward referencing functions in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to wilcro | 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. |
October 16, 2020 Re: Forward referencing functions in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Imperatorn | On Fri, Oct 16, 2020 at 08:04:07PM +0000, Imperatorn via Digitalmars-d-learn wrote: [...] > I think it might be just because you havent defined the function yet at that point. That's not correct; the following works: module mymodule; void func() { forwardfunc(); } void forwardfunc() { } However, this only applies in module scope. If they were declared inside a function body, forwardfunc must be declared before func. T -- It is impossible to make anything foolproof because fools are so ingenious. -- Sammy |
October 16, 2020 Re: Forward referencing functions in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to wilcro | On Friday, 16 October 2020 at 19:55:53 UTC, wilcro 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, and that the following construct is allowable:
>
> void myfunc()
> {
> forwardfunc();
> }
>
> void forwardfunc()
> {
> ... //do stuff
> }
>
>
> However, the following code will cause a compiler error:
>
> import std.stdio: writeln;
>
> void main()
> {
>
> void myfunc() {
>
> forwardfunc(); // onlineapp.d(8): Error: undefined identifier forwardfunc
> }
>
> void forwardfunc() {
>
> writeln("foo");
> }
>
> myfunc();
>
> }
>
>
> Evidently, I am misunderstanding something very elemental here; thanks for any enlightenment regarding this.
Thanks to all for your responses; as a related followup question, would there be any reason to avoid placing the majority of code for a program outside of the main function?
|
October 16, 2020 Re: Forward referencing functions in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to wilcro | On 10/16/20 1:47 PM, wilcro wrote: > would > there be any reason to avoid placing the majority of code for a program > outside of the main function? Keeping scopes of symbols as small as possible is a general guideline in D and elsewhere but I wouldn't crowd my main() function with details of program logic either. (Aside: There is no global name scope in D; everything belongs to a module.) One thing I love about D is that there are no strong principles like that. I code in a way that is comfortable and change things later on as a needs arise. :) Ali |
October 16, 2020 Re: Forward referencing functions in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to wilcro | On 10/16/20 4:47 PM, wilcro wrote:
>
> Thanks to all for your responses; as a related followup question, would there be any reason to avoid placing the majority of code for a program outside of the main function?
Inner functions have benefits:
1. They are only accessible inside the function. Which means you only have to worry about correctness while INSIDE that function.
2. inner functions have access to the outer function's stack frame.
Often, I use inner functions to factor out a common piece of code that I don't want to have to write multiple times in the same function.
-Steve
|
October 16, 2020 Re: Forward referencing functions in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to wilcro | On Friday, 16 October 2020 at 19:55:53 UTC, wilcro 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, and that the following construct is allowable:
>
> void myfunc()
> {
> forwardfunc();
> }
>
> void forwardfunc()
> {
> ... //do stuff
> }
>
>
> However, the following code will cause a compiler error:
>
> import std.stdio: writeln;
>
> void main()
> {
>
> void myfunc() {
>
> forwardfunc(); // onlineapp.d(8): Error: undefined identifier forwardfunc
> }
>
> void forwardfunc() {
>
> writeln("foo");
> }
>
> myfunc();
>
> }
>
>
> Evidently, I am misunderstanding something very elemental here; thanks for any enlightenment regarding this.
Thanks for your insights, Ali and Steve -- very helpful.
|
October 17, 2020 Re: Forward referencing functions in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Friday, 16 October 2020 at 21:28:18 UTC, Steven Schveighoffer wrote:
> Inner functions have benefits:
>
> 1. They are only accessible inside the function. Which means you only have to worry about correctness while INSIDE that function.
> 2. inner functions have access to the outer function's stack frame.
>
> Often, I use inner functions to factor out a common piece of code that I don't want to have to write multiple times in the same function.
>
> -Steve
How can you write two inner functions that call each other? (Recursively)
|
Copyright © 1999-2021 by the D Language Foundation