February 10, 2005 Re: Can't pass delegate to template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote:
> I'm exactly sure why you are using a template, though. You might want to consider using overloaded test functions:
Or here is an, IMHO, prettier template:
import std.stdio;
void main()
{
int foo() { return 3; }
test!(int)(&foo);
}
template test(T)
void test(T delegate() dg)
{
writefln("i = ", dg());
}
|
February 11, 2005 Re: Can't pass delegate to template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | In article <cug9kk$2lkf$1@digitaldaemon.com>, Russ Lewis says... > >Ben Hinkle wrote: >> I'm exactly sure why you are using a template, though. You might want to consider using overloaded test functions: > > > >Or here is an, IMHO, prettier template: > > > >import std.stdio; > >void main() >{ > int foo() { return 3; } > test!(int)(&foo); >} > >template test(T) > void test(T delegate() dg) > { > writefln("i = ", dg()); > } > This works great, thanks (and everyone else's examples)! I get excited with some of D's features and don't always think them through before attempting use; that happens less though the more D I use. I really like this language and hope for D's success. :) Anyway, I do think it would be nice if you were able to extract the return type of a delegate/function. I.e.: test!(typeof(foo.return))(&foo) // something akin to this (or even going further, be able to extract a non-variadic functions parm types which could be resolved at compile-time) I just like genericity in general. I think it makes for more flexible programming IMO. -Kramer |
Copyright © 1999-2021 by the D Language Foundation