www.digitalmars.com         C & C++   DMDScript  

D - D alpha 26

reply "Walter" <walter digitalmars.com> writes:
ftp://www.digitalmars.com/dmdalpha.zip

This implements delegates, described in www.digitalmars.com/d/type.html

It also works under Win98 now.

-Walter
Apr 10 2002
next sibling parent "Pavel Minayev" <evilone omen.ru> writes:
"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...
Apr 10 2002
prev sibling next sibling parent reply "J. Daniel Smith" <j_daniel_smith HoTMaiL.com> writes:
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


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);


    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);


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
Apr 10 2002
parent reply "Walter" <walter digitalmars.com> writes:
"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
  This is needed to easily implement event handling.  But,
 assuming you can make a delegate typedef in D (can you?), the syntax


 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);


     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);


 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
Apr 10 2002
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
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
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))) ]
Apr 10 2002
next sibling parent "Walter" <walter digitalmars.com> writes:
"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

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. which is why I find it confusing.
Apr 10 2002
prev sibling parent reply "J. Daniel Smith" <j_daniel_smith HoTMaiL.com> writes:
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

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))) ]
Apr 11 2002
parent reply "Walter" <walter digitalmars.com> writes:
"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.
Apr 11 2002
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
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))) ]
Apr 11 2002
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
More thoughts about this...the concatenate operator might be useful for
combining
associative arrays/sets; the problem is, what do you do if there are any
conflicting keys?

It might suggest a full set of operators for associative arrays/sets:
    union
    junction
    exclusion (dunno the official name...the backslash operator)

Of course, now we're heading toward a much more complex implementation for sets,
one that might be in a class.  But it seems like a simple and logical extension;
associative arrays lead to sets, and sets lead to built-in set operators.

My 2 cents, anyhow.

--
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))) ]
Apr 11 2002
parent "J. Daniel Smith" <j_daniel_smith HoTMaiL.com> writes:
My sample code for adding multiple delegates is pretty ugly:
        dg_t[] MyEvent;
        // ...
        DelegatesAndEvents.dt_t[0] a_delegate;
        a_delegate[0] = &o.member;
        dae.MyEvent ~= a_delegate;
        a_delegate[0] = &o2.func;
        dae.MyEvent ~= a_delegate;
something like
        dg_t[] MyEvent;
        // ...
        dae.MyEvent += &o.member;
        dae.MyEvent += &o2.func;
would cleaner.

   Dan

"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CB5CB05.8347198C deming-os.org...
 More thoughts about this...the concatenate operator might be useful for
combining
 associative arrays/sets; the problem is, what do you do if there are any
 conflicting keys?

 It might suggest a full set of operators for associative arrays/sets:
     union
     junction
     exclusion (dunno the official name...the backslash operator)

 Of course, now we're heading toward a much more complex implementation for
sets,
 one that might be in a class.  But it seems like a simple and logical
extension;
 associative arrays lead to sets, and sets lead to built-in set operators.

 My 2 cents, anyhow.

 --
 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))) ]
Apr 11 2002
prev sibling parent "OddesE" <OddesE_XYZ hotmail.com> writes:
"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
Apr 10 2002