www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Local function overloading

reply "Philpax" <phillip philliplarkson.com> writes:
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
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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 struct
You 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
parent reply "Philpax" <phillip philliplarkson.com> writes:
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
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 14 April 2014 at 10:35:20 UTC, Jonathan M Davis wrote:
 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
https://issues.dlang.org/show_bug.cgi?id=12578
Apr 14 2014