www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Comparing delegates

reply Markus Dangl <danglm in.tum.de> writes:
Hi,

is it possible to compare delegates?

I'm writing something like an event handling object, where i can add 
delegates for certain events. If i want to remove a delegate, i thought 
it would be the easiest way to go through may array of delegates and 
compare them to the one i want to remove...

Thanks,
Markus
Dec 19 2004
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Markus Dangl wrote:
 Hi,
 
 is it possible to compare delegates?
 
 I'm writing something like an event handling object, where i can add 
 delegates for certain events. If i want to remove a delegate, i thought 
 it would be the easiest way to go through may array of delegates and 
 compare them to the one i want to remove...

Yes. You can also compare delegates to (and set them to) null. I performed a quick testcase. The following program returned 2:
 int main() {
   void delegate() dg = delegate void() {};
   if(dg == delegate void() {})
     return 1;
  
   void delegate()dg2 = dg;
   if(dg == dg2)
     return 2;
 }

Dec 20 2004
next sibling parent reply "Simon Buchan" <currently no.where> writes:
On Mon, 20 Dec 2004 07:38:23 -0700, Russ Lewis  
<spamhole-2001-07-16 deming-os.org> wrote:

 Markus Dangl wrote:
 Hi,
  is it possible to compare delegates?
  I'm writing something like an event handling object, where i can add  
 delegates for certain events. If i want to remove a delegate, i thought  
 it would be the easiest way to go through may array of delegates and  
 compare them to the one i want to remove...

Yes. You can also compare delegates to (and set them to) null. I performed a quick testcase. The following program returned 2:
 int main() {
   void delegate() dg = delegate void() {};
   if(dg == delegate void() {})
     return 1;
    void delegate()dg2 = dg;
   if(dg == dg2)
     return 2;
 }


void delegate() dg = delegate void() {};? There are some things the mind of mortal man is not supposed to ken, at least not at 6.00 AM (Before, not after sleep) But this looks very cool. Just make sure its not because you don't provide a default return value (was it DMD that just returned eax?) -- "Unhappy Microsoft customers have a funny way of becoming Linux, Salesforce.com and Oracle customers." - www.microsoft-watch.com: "The Year in Review: Microsoft Opens Up" -- "I plan on at least one critical patch every month, and I haven't been disappointed." - Adam Hansen, manager of security at Sonnenschein Nath & Rosenthal LLP (Quote from http://www.eweek.com/article2/0,1759,1736104,00.asp) -- "It's been a challenge to "reteach or retrain" Web users to pay for content, said Pizey" -Wired website: "The Incredible Shrinking Comic"
Dec 20 2004
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Simon Buchan wrote:
 void delegate() dg = delegate void() {};?
 There are some things the mind of mortal man is not supposed to
 ken, at least not at 6.00 AM (Before, not after sleep)

This actually has some practical usefulness. Imagine that you define a delegate callback: void delegate() do_this_on_event_foo; void HandleFooEvent() { <do stuff here> do_this_on_event_foo() } How do you set it to do nothing? You have two options. Either add a check in the above code to see if the delegate is null, or else set it to an empty delegate: do_this_on_event_foo = delegate void() { /* do nothing */ }; I like D! :)
 But this looks very cool. Just make sure its not because you don't
 provide a default return value (was it DMD that just returned eax?)

d'oh! Well, I added a 'return 0' at the end, and the executable still returns 2.
Dec 20 2004
prev sibling parent Markus Dangl <danglm in.tum.de> writes:
Russ Lewis schrieb:
 Markus Dangl wrote:
 
 Hi,

 is it possible to compare delegates?

 I'm writing something like an event handling object, where i can add 
 delegates for certain events. If i want to remove a delegate, i 
 thought it would be the easiest way to go through may array of 
 delegates and compare them to the one i want to remove...

Yes. You can also compare delegates to (and set them to) null.

Seems to work for me too, didn't test it though. This is really great for identifying handlers :) Thanks again!
Dec 20 2004