www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - get object back from a delegate?

reply Billy Zelsnack <billy_zelsnack yahoo.com> writes:
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?
Aug 27 2004
next sibling parent Andy Friesen <andy ikagames.com> writes:
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
Aug 27 2004
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
"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.
Aug 28 2004
parent reply teqDruid <me teqdruid.com> writes:
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.
Aug 28 2004
parent "Walter" <newshound digitalmars.com> writes:
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.
Aug 28 2004