A library similar to boost::bind

Kirk McDonald kirklin.mcdonald at gmail.com
Fri Jul 7 10:57:26 PDT 2006


Tom S wrote:
> Lutger wrote:
> 
>> I'm a little late, but thanks!!! I was hoping somebody would do this. 
>> It works
>> like a charm.
>> I tried studying the source, and while it's a LOT better to follow 
>> than boost, I
>> discovered I don't understand metaprogramming at all (yet), unfortunatly.
>> Can I ask you, how can I use the Tuple type and what are it's features?
>> This much I figured out:
>>
>> Tuple!(int,int) someTuple;
>> someTuple.val!(1) = 5;
>> assert(someTuple.val!(1) == 5);
> 
> 
> Thanks for testing and kind words :)
> I've uploaded an updated version at the same address:
> http://158.75.59.9/~h3/code/bind.rar
> 
> 
> As for the Tuple's features, it's really pretty basic, but at the same 
> time quite powerful and allowing for the creation of many interesting 
> templates. I'll use an example to demonstrate its features:
> 
> 
> Tuple!(int, float) a;
> a.val!(0) = 5;
> a.val!(1) = 2.3f;
> 
> // simple printing
> writefln(a.toString);
> 
> 
> // various insertion ops
> auto b = a.prepend!(char[])("foo");
> writefln(b.toString);
> 
> auto c = b.append!(double)(1.234567);
> writefln(c.toString);
> 
> auto d = c.insertAfter!(1, int)(1234);
> writefln(d.toString);
> 
> 
> // .length property like in arrays
> writefln("d.length: ", d.length);
> 
> 
> // behaves like a struct
> auto e = d.insertBefore!(1, char[])("another string...");
> auto f = e;
> f.val!(1) = "modified !";
> 
> writefln(e.toString);
> writefln(f.toString);
> 
> 
> // direct access to fields. the '.val' is an templated alias, not an 
> accessor function
> f.val!(3) += 2;
> writefln(typeid(typeof(f.val!(3))));
> writefln(f.toString);
> 
> 
> struct Test {
>     int x;
>     float y;
>     double z;
>     char w;
> }
> 
> // no extra costs
> static assert(Test.sizeof == (Tuple!(int, float, double, char)).sizeof);
> 
> // easy creation. // "".dup is used because we don't like static arrays.
> writefln(makeTuple(1, 2.2, 5.f, "foo".dup, 2+7i, "bar".dup).toString);
> 
> // nest 'em as you like
> writefln(makeTuple(makeTuple(1, 2), makeTuple(3, 4)).toString);
> 
> 
> 

I've started to write something similar for Pyd, but yours seems more 
full-featured. One useful function of mine is a tuple_from_fn template, 
which derives a tuple type directly from a function type, e.g.:

void func(int i, char[] s) { }

void main() {
     tuple_from_fn!(typeof(&func)) f;
     static assert(is(typeof(f) == tuple!(int, char[])));
}

The closest thing my tuples have to "bind" is a simple 
"apply_tuple_to_fn" function that calls the function with the tuple.

The one question I have is: How well does it handle default function 
arguments? So far I've managed to support them pretty well in Pyd, and I 
don't want to break that.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki



More information about the Digitalmars-d-announce mailing list