binding arbitrary function arguments
BCS
ao at pathlink.com
Thu Jun 28 16:15:51 PDT 2007
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.
|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 Remove!(A).From!(range!(0,vl)) map;
|
| R For(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(arg); // 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 = false;
| 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;
|}
|
|
|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 f(int i, char c, float f) { writef("%s, %s, %s\n", i,c,f); }
|
|void main()
|{
| alias Args! (0,2, 4).
| Become!(1,true,1e7).
| For!(fnc) fn2;
| fn2();
|
| alias T!(int, char, float) A;
| A a;
| a[0] = 0; a[1] = 'c'; a[2] = 1e5;
| f(a);
|}
More information about the Digitalmars-d-learn
mailing list