digitalmars.D - get object back from a delegate?
- Billy Zelsnack (14/14) Aug 27 2004 Is it possible to get the object back from a delegate?
- Andy Friesen (14/31) Aug 27 2004 Presently, you can only do so with vile trickery:
- Walter (7/8) Aug 28 2004 Sure, by using Andy's method. The trouble, though, is there's no type
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
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
"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
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
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:(if"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 objectthethe delegate was formed from a nested function, the 'object' would bestack frame). And worse, if D ever merges the function pointers and delegates into one type, such code would break.
Aug 28 2004