www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Nested functions and their linkage type

reply kdevel <kdevel vogtner.de> writes:
In § 19.18.7 [1] it is said that

    Nested functions always have the D function linkage type.

Why and for what application is that important? As this example

~~~znested.d
void foo () { }
unittest {
    void nested () { }
    import std.stdio;
    writeln (typeof (nested).stringof);
    writeln (typeof (&nested).stringof);
    writeln (typeof (foo).stringof);
    writeln (typeof (&foo).stringof);
}
~~~

    $ dmd -unittest -checkaction=context -main -run znested.d
    pure nothrow  nogc  safe void()
    void delegate() pure nothrow  nogc  safe
    void()
    void function()
    1 unittests passed

shows from the object with D function linkage type named "nested"
a delegate is created as soon as its address is taken. (function
pointers are only generated from static nested functions)

[1] https://dlang.org/spec/function.html#nested
Jan 23 2021
parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 23 January 2021 at 20:37:49 UTC, kdevel wrote:
 In § 19.18.7 [1] it is said that

    Nested functions always have the D function linkage type.

 Why and for what application is that important?
I assume because of mangling. The outer function is mangled into the nested function's name.
 shows from the object with D function linkage type named 
 "nested"
 a delegate is created as soon as its address is taken. (function
 pointers are only generated from static nested functions)
Yes. That's documented here: https://dlang.org/spec/function.html#closures
Jan 23 2021
parent Mike Parker <aldacron gmail.com> writes:
On Sunday, 24 January 2021 at 02:30:37 UTC, Mike Parker wrote:
 On Saturday, 23 January 2021 at 20:37:49 UTC, kdevel wrote:
 In § 19.18.7 [1] it is said that

    Nested functions always have the D function linkage type.

 Why and for what application is that important?
I assume because of mangling. The outer function is mangled into the nested function's name.
And also that nested functions needs to reference the stack of the enclosing function.
Jan 23 2021