www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - toDelegate() for D1

reply %u <e ee.com> writes:
is it available?
Jan 12 2011
parent reply %u <e ee.com> writes:
I only need something to make a void deleg() from a void func().
Jan 12 2011
next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
%u <e ee.com> wrote:

 I only need something to make a void deleg() from a void func().

This works for me: ReturnType!( F ) delegate( ParameterTypeTuple!( F ) ) toDelegate( F )( F fn ) { return ( ParameterTypeTuple!( F ) args ){ return fn( args ); }; } -- Simen
Jan 12 2011
next sibling parent %u <e ee.com> writes:
== Quote from Simen kjaeraas (simen.kjaras gmail.com)'s article
 %u <e ee.com> wrote:
 I only need something to make a void deleg() from a void func().

ReturnType!( F ) delegate( ParameterTypeTuple!( F ) ) toDelegate( F )( F fn ) { return ( ParameterTypeTuple!( F ) args ){ return fn( args ); }; }

I see what you did there(took me a few blinks and spec lookups): function literals default to delegates :) Thanks!!
Jan 12 2011
prev sibling parent reply %u <e ee.com> writes:
== Quote from Simen kjaeraas (simen.kjaras gmail.com)'s article
 %u <e ee.com> wrote:
 I only need something to make a void deleg() from a void func().

ReturnType!( F ) delegate( ParameterTypeTuple!( F ) ) toDelegate( F )( F fn ) { return ( ParameterTypeTuple!( F ) args ){ return fn( args ); }; }

What am I doing wrong. I can't call the delegate twice with anything in between. ---- void func(){ writefln("func"); } void main(){ void delegate() dg; dg = toDelegate(&func); dg(); // np //writefln(dg.ptr," ", dg.funcptr,"."); // Error: Stack Overflow second dg call //writefln(); // Error: Access Violation second dg call dg(); } ----
Jan 13 2011
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
On 13/01/2011 17:14, %u wrote:
 == Quote from Simen kjaeraas (simen.kjaras gmail.com)'s article
 %u<e ee.com>  wrote:
 I only need something to make a void deleg() from a void func().

ReturnType!( F ) delegate( ParameterTypeTuple!( F ) ) toDelegate( F )( F fn ) { return ( ParameterTypeTuple!( F ) args ){ return fn( args ); }; }

What am I doing wrong. I can't call the delegate twice with anything in between.

Every delegate needs a context pointer. If the function is defined within a function, that context pointer points to the stack frame of the function that defined it. And in this case it uses something from that very stack frame: the parameter fn. ISTM it works straight after you've created it only because the stack frame of toDelegate has not yet been overwritten. However, I'm still puzzled, because the process of calling the delegate ought to itself overwrite the stack frame. You want this: http://pr.stewartsplace.org.uk/d/sutil/ Stewart.
Jan 14 2011
prev sibling next sibling parent Moritz Warning <moritzwarning web.de> writes:
On Thu, 13 Jan 2011 17:14:13 +0000, %u wrote:

 == Quote from Simen kjaeraas (simen.kjaras gmail.com)'s article
 %u <e ee.com> wrote:
 I only need something to make a void deleg() from a void func().

ReturnType!( F ) delegate( ParameterTypeTuple!( F ) ) toDelegate( F )( F fn ) { return ( ParameterTypeTuple!( F ) args ){ return fn( args ); }; }

What am I doing wrong. I can't call the delegate twice with anything in between. ---- void func(){ writefln("func"); } void main(){ void delegate() dg; dg = toDelegate(&func); dg(); // np //writefln(dg.ptr," ", dg.funcptr,"."); // Error: Stack Overflow second dg call //writefln(); // Error: Access Violation second dg call dg(); } ----

My tangofied of this code works, maybe it's a lib bug? On the other hand, is the delegate allocated on the stack? Anyway, here is another way: R delegate(T) toDg(R, T...)(R function(T) fp) { struct dg { R opCall(T t) { return (cast(R function(T)) this) (t); } } R delegate(T) t; t.ptr = fp; t.funcptr = &dg.opCall; return t; }
Jan 13 2011
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Moritz Warning <moritzwarning web.de> wrote:

 My tangofied of this code works, maybe it's a lib bug?
 On the other hand, is the delegate allocated on the stack?
 Anyway, here is another way:

 R delegate(T) toDg(R, T...)(R function(T) fp)
 {
     struct dg
     {
         R opCall(T t)
         {
             return (cast(R function(T)) this) (t);
         }
     }
    R delegate(T) t;
     t.ptr = fp;
     t.funcptr = &dg.opCall;
     return t;
 }

Man, that's almost as ugly as some of the things I do. :p That's plenty fine if it works, though. -- Simen
Jan 14 2011