What's left for 1.0?

Kirk McDonald kirklin.mcdonald at gmail.com
Sun Nov 19 16:45:09 PST 2006


Bill Baxter wrote:
> BCS wrote:
>> There is no way to differentiate between function overloads.
>>
>>
>> int foo(){ return 0;}
>> int foo(int i){ return 0;}
>>
>>
>> int bob()
>> {
>>         // foo() or foo(int)?
>>     auto fn = &foo;
>>     auto tmp = TemplateTakingFn!(foo);
>> }
> 
> That should probably be an "error: ambiguous" if it isn't already, but 
> anyway can't you do   'int function() fn = &foo' to get the one you want?
> 
> --bb

I've played with just about every permutation of this problem during the 
course of writing Pyd.

int foo() { return 0; }
int foo(int i) { return 0; }

void main() {
     auto fn = &foo; // Uses the lexically first function
     static assert(is(typeof(fn) == int function()));
     fn();
     //fn(12); // Error: expected 0 arguments, not 1
     int function(int) fn2 = &foo; // Works
     fn2(12);
}

In writing Pyd, I've come to the conclusion that if you have a template 
that accepts an arbitrary function as an alias parameter (and then does 
anything involving the type of that function), you should always have a 
second parameter representing the type of the function. (And you can 
easily make this second parameter have a default value of typeof(&fn).) 
In this way the user can be sure the template is getting the proper 
overload of the function.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org



More information about the Digitalmars-d mailing list