declaration of inner function is already defined

Paul Backus snarwin at gmail.com
Wed May 13 13:26:18 UTC 2020


On Wednesday, 13 May 2020 at 13:18:58 UTC, Andrey wrote:
> On Wednesday, 13 May 2020 at 12:58:11 UTC, Adam D. Ruppe wrote:
>> On Wednesday, 13 May 2020 at 12:45:06 UTC, Andrey wrote:
>>> Why this works:
>>
>> It's just defined that way. Local functions follow local 
>> variable rules - must be declared before use and names not 
>> allowed to overload each other.
>>
>> There might be a deeper reason too but like that's the main 
>> thing, they just work like any other local vars.
>
> Overload for local functions will be very useful thing. 
> Otherwise it is PHP or C.

If you want to define a set of overloaded functions inside 
another function, you can do it like this:

void main() {
     import std.stdio: writeln;

     static struct Overloads {
         static void fun(int n) { writeln("int overload"); }
         static void fun(string s) { writeln("string overload"); }
     }
     alias fun = Overloads.fun;

     fun(123); // int overload
     fun("hello"); // string overload
}


More information about the Digitalmars-d-learn mailing list