Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 10, 2002 D alpha 26 | ||||
---|---|---|---|---|
| ||||
ftp://www.digitalmars.com/dmdalpha.zip This implements delegates, described in www.digitalmars.com/d/type.html It also works under Win98 now. -Walter |
April 10, 2002 Re: D alpha 26 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:a90tao$154a$1@digitaldaemon.com... > This implements delegates, described in www.digitalmars.com/d/type.html OH YES! Expect strong WinD soon... |
April 10, 2002 More on Delegates (was: D alpha 26) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Is the plan to leave the task of maintaining a collection of delegates to the programmer? This is needed to easily implement event handling. But, assuming you can make a delegate typedef in D (can you?), the syntax differences between D and C# are pretty much cosmetic. Here's a simple D sample (the syntax may not be quite right) class DelegatesAndEvents { typedef void delegate(int) dg_t; // dg_t dg; // dg is a delegate to a function dg_t[] MyEvent; void Fire_MyEvent(int event_data) { for (int i=0; i<MyEvent.length; i++) { dg_t e = MyEvent[i]; e(event_data); } } } class OB { void member(int); } class OB2 { void func(int); } OB o = new OB(); OB2 o2 = new OB2(); DelegatesAndEvents dae; DelegatesAndEvents.dt_t[0] a_delegate; a_delegate[0] = &o.member; dae.MyEvent ~= a_delegate; a_delegate[0] = &o2.func; dae.MyEvent ~= a_delegate; dae.Fire_MyEvent(314); And for comparision, the same thing in C# class DelegatesAndEvents { public delegate void dg_t(int); public event dg_t MyEvent; public void Fire_MyEvent(int event_data) { MyEvent(event_data); } }; class OB { void member(int); } class OB2 { void func(int); } OB o = new OB(); OB2 o2 = new OB2(); DelegatesAndEvents dae = new DelegatesAndEvents(); dae.MyEvent += new DelegatesAndEvents.MyEvent(o.member); dae.MyEvent += new DelegatesAndEvents.MyEvent(o2.func); dae.Fire_MyEvent(314); As you can see, the "event" keyword in C# pretty much just makes it easier to build up a collection of delegates; as far as I can tell, that's slightly more cumbersome in D because of having to append arrays. However, there are two important semantic differences between the code snipets: "MyEvent" is better thought of as a set rather than an array, adding the same delegate twice will NOT cause that delegate to get invoked multiple times. The other difference is that in keeping with the "set" data structure, the invocation order is not defined. Dan "Walter" <walter@digitalmars.com> wrote in message news:a90tao$154a$1@digitaldaemon.com... > ftp://www.digitalmars.com/dmdalpha.zip > > This implements delegates, described in www.digitalmars.com/d/type.html > > It also works under Win98 now. > > -Walter > > |
April 10, 2002 Re: D alpha 26 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:a90tao$154a$1@digitaldaemon.com... > ftp://www.digitalmars.com/dmdalpha.zip > > This implements delegates, described in www.digitalmars.com/d/type.html > > It also works under Win98 now. > > -Walter > Thank you! -- Stijn OddesE_XYZ@hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail |
April 10, 2002 Re: More on Delegates (was: D alpha 26) | ||||
---|---|---|---|---|
| ||||
Posted in reply to J. Daniel Smith | "J. Daniel Smith" <j_daniel_smith@HoTMaiL.com> wrote in message news:a91he8$1o7v$1@digitaldaemon.com... > Is the plan to leave the task of maintaining a collection of delegates to the programmer? Yes. A delegate and a set are two distinct things, and I don't think they should be mushed together. I find the C# syntax to be very confusing. > This is needed to easily implement event handling. But, > assuming you can make a delegate typedef in D (can you?), the syntax > differences between D and C# are pretty much cosmetic. > > Here's a simple D sample (the syntax may not be quite right) > class DelegatesAndEvents > { > typedef void delegate(int) dg_t; // dg_t dg; // dg is a delegate > to a function > dg_t[] MyEvent; > > void Fire_MyEvent(int event_data) { > for (int i=0; i<MyEvent.length; i++) > { > dg_t e = MyEvent[i]; > e(event_data); > } > } > } > class OB { void member(int); } > class OB2 { void func(int); } > > OB o = new OB(); > OB2 o2 = new OB2(); > DelegatesAndEvents dae; > DelegatesAndEvents.dt_t[0] a_delegate; > a_delegate[0] = &o.member; > dae.MyEvent ~= a_delegate; > a_delegate[0] = &o2.func; > dae.MyEvent ~= a_delegate; > > dae.Fire_MyEvent(314); > > And for comparision, the same thing in C# > class DelegatesAndEvents > { > public delegate void dg_t(int); > public event dg_t MyEvent; > > public void Fire_MyEvent(int event_data) { > MyEvent(event_data); > } > }; > class OB { void member(int); } > class OB2 { void func(int); } > > OB o = new OB(); > OB2 o2 = new OB2(); > > DelegatesAndEvents dae = new DelegatesAndEvents(); > dae.MyEvent += new DelegatesAndEvents.MyEvent(o.member); > dae.MyEvent += new DelegatesAndEvents.MyEvent(o2.func); > > dae.Fire_MyEvent(314); > > As you can see, the "event" keyword in C# pretty much just makes it easier to build up a collection of delegates; as far as I can tell, that's slightly > more cumbersome in D because of having to append arrays. > > However, there are two important semantic differences between the code snipets: "MyEvent" is better thought of as a set rather than an array, adding the same delegate twice will NOT cause that delegate to get invoked multiple times. The other difference is that in keeping with the "set" data > structure, the invocation order is not defined. > > Dan > > "Walter" <walter@digitalmars.com> wrote in message news:a90tao$154a$1@digitaldaemon.com... > > ftp://www.digitalmars.com/dmdalpha.zip > > > > This implements delegates, described in www.digitalmars.com/d/type.html > > > > It also works under Win98 now. > > > > -Walter > > > > > > |
April 10, 2002 Re: More on Delegates (was: D alpha 26) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > "J. Daniel Smith" <j_daniel_smith@HoTMaiL.com> wrote in message news:a91he8$1o7v$1@digitaldaemon.com... > > Is the plan to leave the task of maintaining a collection of delegates to the programmer? > > Yes. A delegate and a set are two distinct things, and I don't think they should be mushed together. I find the C# syntax to be very confusing. Doesn't seem too hard to declare an array of delegates and have a the caller iterate through it when an event happens... -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
April 11, 2002 Re: More on Delegates (was: D alpha 26) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3CB4CA05.915224EF@deming-os.org... > Walter wrote: > > "J. Daniel Smith" <j_daniel_smith@HoTMaiL.com> wrote in message news:a91he8$1o7v$1@digitaldaemon.com... > > > Is the plan to leave the task of maintaining a collection of delegates to > > > the programmer? > > Yes. A delegate and a set are two distinct things, and I don't think they > > should be mushed together. I find the C# syntax to be very confusing. > Doesn't seem too hard to declare an array of delegates and have a the caller > iterate through it when an event happens... I agree, and with such code you can see and understand what's happening. With C#, much of what happens is outside the normal rules of the syntax, which is why I find it confusing. |
April 11, 2002 Re: More on Delegates (was: D alpha 26) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | Well, other than that for event handling you normally want a set, not an array. This makes it easy for multiple places in the code to add the same event handler without the worry of it getting called multiple times. I guess leaving it up to the programmer makes it easier to handle situations where you really want the delelgates invoked in the same (or reverse) order as they were added. Is there an easy way to do set manipulation in D, or is that the responsibility of some library class? Dan "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3CB4CA05.915224EF@deming-os.org... > Walter wrote: > > > "J. Daniel Smith" <j_daniel_smith@HoTMaiL.com> wrote in message news:a91he8$1o7v$1@digitaldaemon.com... > > > Is the plan to leave the task of maintaining a collection of delegates to > > > the programmer? > > > > Yes. A delegate and a set are two distinct things, and I don't think they > > should be mushed together. I find the C# syntax to be very confusing. > > Doesn't seem too hard to declare an array of delegates and have a the caller > iterate through it when an event happens... > > -- > The Villagers are Online! villagersonline.com > > .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] > .[ (a version.of(English).(precise.more)) is(possible) ] > ?[ you want.to(help(develop(it))) ] > > |
April 11, 2002 Re: More on Delegates (was: D alpha 26) | ||||
---|---|---|---|---|
| ||||
Posted in reply to J. Daniel Smith | "J. Daniel Smith" <j_daniel_smith@HoTMaiL.com> wrote in message news:a940r6$1d86$1@digitaldaemon.com... > Is there an easy way to do set manipulation in D, or is that the responsibility of some library class? Probably the easiest way is to use an associative array. Use the delegate as both the key and the value. That way each unique delegate will only appear once in the associative array. |
April 11, 2002 Re: More on Delegates (was: D alpha 26) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > "J. Daniel Smith" <j_daniel_smith@HoTMaiL.com> wrote in message news:a940r6$1d86$1@digitaldaemon.com... > > Is there an easy way to do set manipulation in D, or is that the responsibility of some library class? > > Probably the easiest way is to use an associative array. Use the delegate as both the key and the value. That way each unique delegate will only appear once in the associative array. That would work. Or you could have an associative array with the delegate as the key and some kind of info about it in the data... Of course, maybe you don't need any data, so you just include a bool or int value that is never accessed... Oh, wait, now we're talking about a set, implemented by the compiler, but with the wasted space of all those junk variables... Maybe it would make sense to leverage existing associative array code and give us a set fundamental type? :) It can have size, length, rehash, and keys properties, all like an associative array. It just wouldn't have a values property... -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
Copyright © 1999-2021 by the D Language Foundation