Thread overview
get object back from a delegate?
Aug 28, 2004
Billy Zelsnack
Aug 28, 2004
Andy Friesen
Aug 28, 2004
Walter
Aug 28, 2004
teqDruid
Aug 29, 2004
Walter
August 28, 2004
Is it possible to get the object back from a delegate?

ie.

class Monkey
{
  void vomit(float value)
  {
  }
}

Monkey monkeyMan=new Monkey();
void delegate(float value) vomit2;
vomit2=&monkeyMan.vomit;
vomit2(3);

Can I get the 'monkeyMan' object back from the 'vomit2' delegate?
August 28, 2004
Billy Zelsnack wrote:
> Is it possible to get the object back from a delegate?
> 
> ie.
> 
> class Monkey
> {
>   void vomit(float value)
>   {
>   }
> }
> 
> Monkey monkeyMan=new Monkey();
> void delegate(float value) vomit2;
> vomit2=&monkeyMan.vomit;
> vomit2(3);
> 
> Can I get the 'monkeyMan' object back from the 'vomit2' delegate?

Presently, you can only do so with vile trickery:

    union VileTrickery {
        void delegate(float value) asDelegate;
        struct {
            void* instance;
            void* method;
        }
    }

    VileTrickery vt;
    vt.asDelegate = &monkeyMan.vomit;
    Monkey m = cast(Monkey)vt.instance;

This is not guaranteed to be the slightest bit portable.

 -- andy
August 28, 2004
"Billy Zelsnack" <billy_zelsnack@yahoo.com> wrote in message news:cgojb7$25hp$1@digitaldaemon.com...
> Is it possible to get the object back from a delegate?

Sure, by using Andy's method. The trouble, though, is there's no type information for what that object is, and it might not even be an object (if the delegate was formed from a nested function, the 'object' would be the stack frame). And worse, if D ever merges the function pointers and delegates into one type, such code would break.


August 28, 2004
What are your plans concerning merging fp's and delegates?  It'd be nice.

I apologize if this has been asked and answered before.

John

On Sat, 28 Aug 2004 11:20:31 -0700, Walter wrote:

> 
> "Billy Zelsnack" <billy_zelsnack@yahoo.com> wrote in message news:cgojb7$25hp$1@digitaldaemon.com...
>> Is it possible to get the object back from a delegate?
> 
> Sure, by using Andy's method. The trouble, though, is there's no type information for what that object is, and it might not even be an object (if the delegate was formed from a nested function, the 'object' would be the stack frame). And worse, if D ever merges the function pointers and delegates into one type, such code would break.

August 29, 2004
I know how to do it, it just got put off.

"teqDruid" <me@teqdruid.com> wrote in message news:pan.2004.08.28.19.50.38.110564@teqdruid.com...
> What are your plans concerning merging fp's and delegates?  It'd be nice.
>
> I apologize if this has been asked and answered before.
>
> John
>
> On Sat, 28 Aug 2004 11:20:31 -0700, Walter wrote:
>
> >
> > "Billy Zelsnack" <billy_zelsnack@yahoo.com> wrote in message news:cgojb7$25hp$1@digitaldaemon.com...
> >> Is it possible to get the object back from a delegate?
> >
> > Sure, by using Andy's method. The trouble, though, is there's no type information for what that object is, and it might not even be an object
(if
> > the delegate was formed from a nested function, the 'object' would be
the
> > stack frame). And worse, if D ever merges the function pointers and delegates into one type, such code would break.
>