www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Limited and easy template argument deduction

reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
I have an idea for how you could do template parameter deduction (to 
make it easy to call template functions) that would not require any 
compiler complexity.

Let's define a special case.  If your:
* function name matches the name of the template
* argument count in the function is greater than or equal to the 
parameter count of the template
* the first n arguments of the function take exactly the template 
parameters, in order

Then the template's parameters are assumed to be typeof() the various 
arguments.  For example, the code below would be legal:



template foo(T1,T2) void foo(T1 dg1,T2 dg2, int val3) {
   writefln("val1 = ", dg1());
   writefln("val2 = ", dg2());
   writefln("val3 = ", val3);
}
void main() {
   int func1() { return 1; };
   int func2() { return 2.0; };

   foo(&func1, &func2, 100);
}



The call in main() would be equivalent to this line:
   foo!(typeof(&func1),typeof(&func2))(&func1, &func2, 100);



This wouldn't require hardly any compiler complexity, and yet would make 
D's templates far easier to use.
Feb 11 2005
parent reply "Walter" <newshound digitalmars.com> writes:
While your idea is a good one and will work, if D gets implicit argument
deduction, it will be better than C++'s instead of limited <g>.
Feb 11 2005
next sibling parent Norbert Nemec <Norbert Nemec-online.de> writes:
Walter wrote:

 While your idea is a good one and will work, if D gets implicit argument
 deduction, it will be better than C++'s instead of limited <g>.
Hopefully, this 'if' is not too far away from a 'when'... :-)
Feb 12 2005
prev sibling next sibling parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Walter wrote:
 While your idea is a good one and will work, if D gets implicit argument
 deduction, it will be better than C++'s instead of limited <g>.
Having watched D grow, I'm not surprised. :) However, is it fair to assume that the special case I described will at least be a subset of the final design? If so, IMHO, it would be good to implement at least this little bit now because it would be easy to do but would have substantial positive impact. If, however, you're not yet certain that the final design would work like this subset, then I understand holding off.
Feb 14 2005
prev sibling parent Matthias Becker <Matthias_member pathlink.com> writes:
While your idea is a good one and will work, if D gets implicit argument
deduction, it will be better than C++'s instead of limited <g>.
Do you already have any ideas?
Feb 14 2005