D 2.0 - dalegates to function

Reiner Pope reIGNOREiner.poCAPSpe at gmFOOail.cBARom
Thu Mar 1 03:53:12 PST 2007


sergk wrote:
> Perhaps it's time to to bump an old request postponed to 2.0 times =)
> 
> So, how about to add some thunking magic - how  complex/complicated to implement it?
> 
This works, except for the inout issue as Frits mentioned:

> T delegate(U) thunkify(T, U...)(T function(U) f)
> {
>     struct Foo
>     {
>         T function(U) _f;
>         T run(U u) { return _f(u); }
>     }
>     Foo* value = new Foo;
>     value._f = f;
>     return &value.run;
> }

Called like:

> int add2(int num)
> {
>     return num + 2;
> }
> 
> void main()
> {
>     auto dg = thunkify(&add2);
> }

Cheers,

Reiner



More information about the Digitalmars-d mailing list