Thread overview
toDelegate() for D1
Jan 12, 2011
%u
Jan 12, 2011
%u
Jan 12, 2011
Simen kjaeraas
Jan 12, 2011
%u
Jan 13, 2011
%u
Jan 14, 2011
Moritz Warning
Jan 15, 2011
Simen kjaeraas
Jan 14, 2011
Stewart Gordon
January 12, 2011
is it available?
January 12, 2011
I only need something to make a void deleg() from a void func().
January 12, 2011
%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
January 12, 2011
== 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().
> This works for me:
> 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!!
January 13, 2011
== 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().
> This works for me:
> 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();
}
----
January 14, 2011
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().
>> This works for me:
>> 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;
}
January 14, 2011
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().
>> This works for me:
>> 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.
January 15, 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