www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - declaration of inner function is already defined

reply Andrey <saasecondbox yandex.ru> writes:
Hi,

Why this works:
 void setBases(string type)(ref int data, string base, string[] 
 syllables)
 {
 
 }
 
 void setBases(string type, T)(ref int data, const ref T source)
 {
 
 }
 
 void main()
 {
     int q = 6;
     setBases!"tt"(q, "qwerty", ["tg", "jj"]);
     setBases!"tt"(q, q);
 }
and this doesn't work:
 void main()
 {
     void setBases(string type)(ref int data, string base, 
 string[] syllables)
     {
 
     }
 
     void setBases(string type, T)(ref int data, const ref T 
 source)
     {
 
     }
 
     int q = 6;
     setBases!"tt"(q, "qwerty", ["tg", "jj"]);
     setBases!"tt"(q, q);
 }
The error is:
 declaration setBases(string type, T)(ref int data, ref const T 
 source) is already defined
?
May 13 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
May 13 2020
parent reply Andrey <saasecondbox yandex.ru> writes:
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.
May 13 2020
parent Paul Backus <snarwin gmail.com> writes:
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 }
May 13 2020