digitalmars.D.learn - declaration of inner function is already defined
- Andrey (5/42) May 13 2020 and this doesn't work:
- Adam D. Ruppe (6/7) May 13 2020 It's just defined that way. Local functions follow local variable
- Andrey (3/10) May 13 2020 Overload for local functions will be very useful thing. Otherwise
- Paul Backus (13/25) May 13 2020 If you want to define a set of overloaded functions inside
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
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
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:Overload for local functions will be very useful thing. Otherwise it is PHP or C.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
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: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 }On Wednesday, 13 May 2020 at 12:45:06 UTC, Andrey wrote:Overload for local functions will be very useful thing. Otherwise it is PHP or C.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