Nested functions and their linkage type

kdevel kdevel at vogtner.de
Sat Jan 23 20:37:49 UTC 2021


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


More information about the Digitalmars-d-learn mailing list