digitalmars.D - Comparing delegates
- Markus Dangl (8/8) Dec 19 2004 Hi,
- Russ Lewis (3/20) Dec 20 2004 Yes. You can also compare delegates to (and set them to) null.
- Simon Buchan (20/37) Dec 20 2004 void delegate() dg = delegate void() {};?
- Russ Lewis (15/20) Dec 20 2004 This actually has some practical usefulness. Imagine that you define a
- Markus Dangl (4/18) Dec 20 2004 Seems to work for me too, didn't test it though.
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
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
On Mon, 20 Dec 2004 07:38:23 -0700, Russ Lewis <spamhole-2001-07-16 deming-os.org> wrote:Markus Dangl 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) 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"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
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
Russ Lewis schrieb:Markus Dangl wrote:Seems to work for me too, didn't test it though. This is really great for identifying handlers :) Thanks again!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.
Dec 20 2004