digitalmars.D.learn - toDelegate() for D1
- %u (1/1) Jan 12 2011 is it available?
- %u (1/1) Jan 12 2011 I only need something to make a void deleg() from a void func().
- Simen kjaeraas (8/9) Jan 12 2011 This works for me:
- %u (4/11) Jan 12 2011 I see what you did there(took me a few blinks and spec lookups): functio...
- %u (13/20) Jan 13 2011 What am I doing wrong. I can't call the delegate twice with anything in ...
- Moritz Warning (19/43) Jan 13 2011 My tangofied of this code works, maybe it's a lib bug?
- Simen kjaeraas (5/22) Jan 14 2011 Man, that's almost as ugly as some of the things I do. :p
- Stewart Gordon (12/21) Jan 14 2011 Every delegate needs a context pointer. If the function is defined
%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
== Quote from Simen kjaeraas (simen.kjaras gmail.com)'s article%u <e ee.com> wrote:I see what you did there(took me a few blinks and spec lookups): function literals default to delegates :) Thanks!!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 ); }; }
Jan 12 2011
== Quote from Simen kjaeraas (simen.kjaras gmail.com)'s article%u <e ee.com> wrote: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(); } ----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 ); }; }
Jan 13 2011
On Thu, 13 Jan 2011 17:14:13 +0000, %u wrote:== Quote from Simen kjaeraas (simen.kjaras gmail.com)'s articleMy 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; }%u <e ee.com> wrote: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(); } ----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 ); }; }
Jan 13 2011
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
On 13/01/2011 17:14, %u wrote:== Quote from Simen kjaeraas (simen.kjaras gmail.com)'s articleEvery 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.%u<e ee.com> wrote:What am I doing wrong. I can't call the delegate twice with anything in between.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 ); }; }
Jan 14 2011