Polymorphism problem w/local functions?

Jonathan M Davis jmdavisProg at gmx.com
Mon Jul 18 09:10:47 PDT 2011


On Monday 18 July 2011 14:11:29 Magnus Lie Hetland wrote:
> Is it intended that local functions can't be polymorphic?
> 
> Example:
> 
> void foo(int x) {}
> void foo(string x) {}
> 
> void bar() {
>     void foo(int x) {}
>     void foo(string x) {}
> }
> 
> void main() {
> }
> 
> The error (at line 6) is "declaration foo is already defined".
> 
> The code compiles if you comment out at least one of the local
> functions (but not if you, for example, comment out the global ones, of
> course).
> 
> Is this a bug, or am I just missing the reasoning behind it? Any
> workarounds? (I'm still at 2.052, so maybe this works in the new
> version?)

Well, technically-speaking, that's not really polymorphism, since the choice 
of function is decided at compile time (polymorphism would be dealing with 
overridden functions than overloaded ones), but I suppose that that's not 
really here nor there.

In any case, no you can't overload nested functions. You've never been able 
to, and you still can't do it. I don't know _why_ such a restriction exists, 
but it does. Feel free to open up an enhancement request for it. I don't know 
that it'll do much good, but maybe you'll luck out. Not knowing why the 
restriction exists in the first place, I don't know what the chances are of 
that restriction being removed. For all I know, it's an Walter's TODO list.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list