Thread overview
Nested functions and their linkage type
Jan 23, 2021
kdevel
Jan 24, 2021
Mike Parker
Jan 24, 2021
Mike Parker
January 23, 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
January 24, 2021
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

January 24, 2021
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.