A functor-based curry: YET I HAVE PROBLEM

Im, Jihyuk icedac at g-nospam-mail.com
Tue Jan 29 20:43:34 PST 2008


thanx for your kind reply :)

now I get to know the problem was a function template block should has one 
definition inside.
but I think if the second code works fine, I think it's useful for
listing template parameter once and use various tempalte declaration but the 
parameter deduced with parameters to
template function inside.

Indeed my third code works fine for me :) your code contains string code
evaluation and mixin that's yet tough to me as c++ programmer

But i found this simple code also emits duduction failure error

[code]
template Foo( R, A, U  )
{
 R delegate (U)  Foo( R delegate ( A, U ) dg, A a )
 {
  R Foo_( U u )
  { R i; return i; }
  return &Foo2_;
 }
}

void my_test()
{
 int plus_3( int a, int b, int c )
 {
  return a+b+c;
 }

 Foo( &plus_3, 10 );  // deduction failure
}
[/code]

in simpler function template works fine like:

[code]
R delegate (U) Foo2( R, A, U...)( R delegate ( A, U ) dg, A a )
{
 R Foo_( U u )
 { R i; return i; }
 return &Foo2_;
}
 Foo2( &plus_3, 10 );  // ok
[/code]

What's the problem? bug? or is there anything that i couldn't get yet?

"downs" <default_357-line at yahoo.de> wrote in message 
news:fnncoj$2g95$1 at digitalmars.com...
> [snip]
>
> Your example doesn't work because a function template must only contain 
> exactly a single defined symbol that has the same name as the template.
>
> Here's a simple functor version:
>
>> import std.stdio, std.traits;
>>
>
> A sample function
>
>> int add(int a, int b, int c) { return a+b+c; }
>>
>
> The initialization of T
>
>> template Init(T) { T Init; }
>>
>> class CurryFunctor(C, P...) {
>>   C callable;
>>   P params;
>>   this(C c, P p) {
>>     callable = c;
>>     foreach (id, v; p) params[id] = v;
>>   }
>
> The purpose of this is to allow a kind of automatic return type deduction.
> Think of this as the function body of opCall.
>
>>   const string CODE = "
>>     static if (is(typeof(callable(params, Init!(R))))) {
>>       return callable(params, rest);
>>     } else
>>       return curry(callable, params, rest);
>>   ";
>
> Now we use our function body to determine the return type of opCall, by 
> asking the question:
> "If our function were a function literal that took no parameters 
> (declaring rest as a variable;
> this is okay because this function literal is never called), then what 
> would its type be?"
>
>>   typeof(mixin("function(){ R rest; "~CODE~" }()")) opCall(R...)(R rest) 
>> {
>>     mixin(CODE);
>>   }
>> }
>>
>
> A simple wrapper.
>
>> CurryFunctor!(C, P) curry(C, P...)(C callable, P params) {
>>   return new CurryFunctor!(C, P)(callable, params);
>> }
>>
>
> And examples.
>
>> void main() {
>>   writefln(curry(&add, 2, 3)(5));
>>   writefln(curry(&add, 2)(3)(4));
>> }
>
> Have fun with it!
>
> --downs 




More information about the Digitalmars-d-learn mailing list