binding arbitrary function arguments

BCS ao at pathlink.com
Thu Jul 5 11:35:03 PDT 2007


Reply to Benjamin,

> Ohh this is a good one!!
> bind.d(32): function bind.fnc (int,char,bool,int[],float) does not
> match
> parameter types ((int, char, bool, int[], float))
> BTW this almost does arbitrary binding.
> 

I am a compleat idiot!!! I was trying to pass a type tuple to a function. 
Obviously that shouldn't work!!!

(p.s. the message isn't that helpful, something like "can't pass non value 
parameters to function" would be nice)


This works

>import std.stdio;
>
>int fnc(int that, char has, bool lots, int[] of, float args)
>{
>   writef("%s, %s, %s, %s, %s\n",that,has,lots,of,args);
>   return 0;
>}
>
>void main()
>{
>   alias   Args!  (0,2,   4).
>      Become!(1,true,1e7).
>      For!(fnc).Gives fn2;
>   fn2('c', [1,2,3]);
>}
>
>
>template T(t...){alias t T;}
>
>template Args(A...)
>{
>   template Become(V...)
>   {
>      static assert(A.length == V.length);
>      template For(alias fn)
>      {
>         static if(
>            is(typeof(fn) arg == function) &&
>            is(typeof(fn) R   == return)
>            )
>         {
>            alias Remove!(A).From!(arg) pArgs;
>            static const uint vl = V.length;
>            alias range!(0,vl) imap;
>            alias Remove!(A).From!(imap) map;
>
>            R Gives(pArgs p)
>            {
>               arg set;
>               foreach(uint i, v; A)
>               {
>                  set[A[i]] = V[i];
>               }
>
>               foreach(uint i, v; map)
>               {
>                  set[map[i]] = p[i];
>               }
>
>               return fn(set);      // line 34
>            }
>         }
>         else
>            static assert(false);
>      }
>   }
>}
>
>
>template Remove(R...)
>{
>   template From(A...)
>   {
>      static if(A.length != 0)
>      {
>         static if(isIn!(A.length-1, R))
>         {
>            alias Remove!(R).From!(A[0..$-1]) From;
>         }
>         else
>         {
>            alias T!(Remove!(R).From!(A[0..$-1]), A[$-1]) From;
>         }
>      }
>      else
>      {
>         alias T!() From;
>      }
>
>   }
>}
>
>template isIn(A...)
>{
>   static assert(A.length != 0);
>
>   static if(A.length == 1)
>      const bool isIn = false;
>   else
>   {
>      static if(A[0] == A[$-1])
>         const bool isIn = true;
>      else
>         const bool isIn = isIn!(A[0], A[1..$-1]);
>   }
>}
>
>template range(int start, int stop, A...)
>{
>   static if(start >= stop) 
>      alias T!(A,start) range;
>   else
>      alias range!(start+1, stop, A, start) range;
>}




More information about the Digitalmars-d-learn mailing list