digitalmars.D.learn - Local function overloading
- Philpax (5/5) Apr 12 2014 While trying to overload a function in local/function scope, I
- monarch_dodra (18/24) Apr 12 2014 I *think* it has something to do with how name-mangling is done.
- Andrej Mitrovic (15/17) Apr 12 2014 You can also use a mixin template:
- Philpax (11/11) Apr 13 2014 Thanks! I used the static struct solution.
- Jonathan M Davis (8/10) Apr 14 2014 Yes. IIRC, I complained about it at one point, and Walter didn't like th...
- monarch_dodra (2/16) Apr 14 2014 https://issues.dlang.org/show_bug.cgi?id=12578
While trying to overload a function in local/function scope, I ran into this behaviour: http://dpaste.dzfl.pl/b4e8b9ddf78a and I was wondering what the cause was. As far as I can tell, this should be fine in global scope (and it is), but I'm curious as to why it doesn't work inside a function.
Apr 12 2014
On Saturday, 12 April 2014 at 16:45:02 UTC, Philpax wrote:While trying to overload a function in local/function scope, I ran into this behaviour: http://dpaste.dzfl.pl/b4e8b9ddf78a and I was wondering what the cause was. As far as I can tell, this should be fine in global scope (and it is), but I'm curious as to why it doesn't work inside a function.I *think* it has something to do with how name-mangling is done. I don't know the details. I know you can workaround it by putting your functions a static members of a dummy struct: http://dpaste.dzfl.pl/268e3d2d4427 class A {} class B {} void main() { static struct Dummy { static void func2(A a){} static void func2(B b){} } alias func2 = Dummy.func2; A a; func2(a); }
Apr 12 2014
On 4/12/14, monarch_dodra <monarchdodra gmail.com> wrote:I know you can workaround it by putting your functions a static members of a dummy structYou can also use a mixin template: class A {} class B {} mixin template M() { void func2(A a) { } void func2(B b) { } } void main() { mixin M!(); func2(new A); func2(new B); }
Apr 12 2014
Thanks! I used the static struct solution. I did some quick research, and I think that the reason why the original code doesn't work is because the two functions have the same identifier, which results in Dsymboltable::insert rejecting the second function. I haven't tested this hypothesis, but I suspect this to be the case. A simple solution would be to assign unique identifiers to each function, but I haven't experimented with DMD source enough to determine what the side-effects of such a change would be. Is not being able to overload functions in local scope intended behaviour?
Apr 13 2014
On Sunday, April 13, 2014 08:40:17 Philpax wrote:Is not being able to overload functions in local scope intended behaviour?Yes. IIRC, I complained about it at one point, and Walter didn't like the idea of having overloaded nested functions. I don't remember what his reasoning was, but I don't remember agreeing with it either. You can certainly always open up an enhancement request for it. Maybe one of the compiler devs can implement it and talk Walter into accepting it: https://issues.dlang.org/ - Jonathan M Davis
Apr 14 2014
On Monday, 14 April 2014 at 10:35:20 UTC, Jonathan M Davis wrote:On Sunday, April 13, 2014 08:40:17 Philpax wrote:https://issues.dlang.org/show_bug.cgi?id=12578Is not being able to overload functions in local scope intended behaviour?Yes. IIRC, I complained about it at one point, and Walter didn't like the idea of having overloaded nested functions. I don't remember what his reasoning was, but I don't remember agreeing with it either. You can certainly always open up an enhancement request for it. Maybe one of the compiler devs can implement it and talk Walter into accepting it: https://issues.dlang.org/ - Jonathan M Davis
Apr 14 2014