1.0 ?? [templates and currying]
Kirk McDonald
kirklin.mcdonald at gmail.com
Tue Nov 7 10:28:39 PST 2006
BCS wrote:
> While the topic is function, "there arguments and what not" could we get
> a fix to issue #52 (I think that is the number). As it stands, there is
> no way to reference an overloaded function with out already knowing it's
> type.
>
> int foo(char c){return 0;}
> void foo(long l){}
>
> auto fn = &foo;
> // the foo used changes if the order of the above changes.
>
> this could also cause problems while attempting to call overloaded
> functions the an overloaded function as a parameter
>
> void fuf(int function(char) fn){}
> void fuf(void function(long) fn){}
>
> fuf(&foo); // what is used here
>
Pyd grapples with this problem by passing around all "real" functions as
a combination of function alias and function type, which are typically
referenced as fn and fn_t, respectively, in the code.
Given your overloads of foo, above, saying
def!(foo);
will wrap the first overload in Pyd. To wrap the other overload, the
user has to explicitly provide its type (and an alternate name):
def!(foo, "foo2", void function(long));
A related problem is the issue of default arguments.
void bar(int i, real r=5.8, char[] s="hello") {}
typeof(&bar) is void function(int, real, char[]). Attempting to say
void function(int) fn = &bar;
does not work. (It fails to compile as written. Casting &bar to the
proper type allows it to compile, but results in odd behavior.)
Similarly, saying
void function(int, real, char[]) fn = &bar;
fn(1);
doesn't work. (Calling fn with the wrong number of arguments is an error.)
One of the additions I made to the metaprogramming library I use in Pyd
is a simple little array of template functions that allows you to get a
function pointer calling the first n arguments of a function. The
original version used a mass of templates; the new variadics would allow
it to look something like this:
template firstArgsT(alias fn, uint i, fn_t) {
alias RetType!(fn_t) R;
alias tupleFromFn_T!(fn_t)[0..i] T; // A theoretical template
R func(T t) {
return fn(t);
}
}
template firstArgs(alias fn, uint i, fn_t = typeof(&fn)) {
alias firstArgsT!(fn, i, fn_t).func firstArgs;
}
Some sort of streamlining of this process would be appreciated as well.
--
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org
More information about the Digitalmars-d
mailing list