www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Problems with implicit template instantiation: Tail Currying

reply Russell Lewis <webmaster villagersonline.com> writes:
I'm trying to write a template to perform function currying of the 
*last* argument to a delegate, and I can't get implicit instantiation to 
work with my template.  Take a look at this example code:

=======
void delegate(U) Curry    (A,U...)(void delegate(A,U) dg,A arg) {
   return delegate void(U) {};
}
void delegate(U) CurryTail(A,U...)(void delegate(U,A) dg,A arg) {
   return delegate void(U) {};
}
void foo() {
   void delegate(uint,char[],uint) dg;
   uint i;
   Curry    (dg,i);
   CurryTail(dg,i);
}
=======

The CurryTail call fails with the following errors:

foo.d(11): template foo.CurryTail(A,U...) does not match any template 
declaration
foo.d(11): template foo.CurryTail(A,U...) cannot deduce template 
function from argument types (void delegate(uint, char[], uint),uint)



So: if the Curry() call works, why doesn't the CurryTail() call?  Is 
this a bug, or some sort of design restriction?

Russ
Jul 18 2007
parent downs <default_357-line yahoo.de> writes:
Russell Lewis wrote:
 I'm trying to write a template to perform function currying of the 
 *last* argument to a delegate, and I can't get implicit instantiation to 
 work with my template.  Take a look at this example code:
 
 =======
 void delegate(U) Curry    (A,U...)(void delegate(A,U) dg,A arg) {
   return delegate void(U) {};
 }
 void delegate(U) CurryTail(A,U...)(void delegate(U,A) dg,A arg) {
   return delegate void(U) {};
 }
 void foo() {
   void delegate(uint,char[],uint) dg;
   uint i;
   Curry    (dg,i);
   CurryTail(dg,i);
 }
 =======
 
 The CurryTail call fails with the following errors:
 
 foo.d(11): template foo.CurryTail(A,U...) does not match any template 
 declaration
 foo.d(11): template foo.CurryTail(A,U...) cannot deduce template 
 function from argument types (void delegate(uint, char[], uint),uint)
 
 
 
 So: if the Curry() call works, why doesn't the CurryTail() call?  Is 
 this a bug, or some sort of design restriction?
 
 Russ

void delegate(T[0..$-1]) CurryTail(P, T...) (void delegate(T) dg, P arg) { static assert(is(P: T[$-1]), "Invalid type: "~P.toString~" cannot be converted to "~T[$-1].toString); return delegate void(T[0..$-1] t) {}; }
Jul 18 2007