April 02, 2002 Re: new D site | ||||
---|---|---|---|---|
| ||||
Posted in reply to J. Daniel Smith Attachments:
| "J. Daniel Smith" <j_daniel_smith@HoTMaiL.com> wrote in message news:a8d5d1$2fic$1@digitaldaemon.com... Here's some C++ code snippets to demonstrate how delegates/events work in C#. Note that C# borrows heavily for Java: there are no global functions, everything is in some class. But C# allows static member functions (which are essentially the same as globals) to be used as delegates. |
April 02, 2002 Re: new D site | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | I think the C# compiler uses some reflection stuff so that it can do typechecking at compile time. With some effort in C++, you can do it all with templates; but that's not an option in either C# or D. Dan "Pavel Minayev" <evilone@omen.ru> wrote in message news:a8d552$2fc7$1@digitaldaemon.com... > "Walter" <walter@digitalmars.com> wrote in message news:a8d3t1$2em9$1@digitaldaemon.com... > > > Is it expected to work with non-member functions too, with member > functions > > arbitrarilly mixed in? > > It would be just great (and it is the way C# delegates work). Practically, internals could look like this: > > // format of delegate pointer > struct Delegate > { > Object obj; > void* func; > } > > > > class Form { void bar(int n); } > Form foo; > void baz(int n); > delegate void ptr(int); // declare delegate pointer > > ptr = &foo.bar; // store pointer to method bar() of object foo > /* > Implemented as: > ptr.obj = foo; > ptr.func = &foo.classinfo.vtbl[bar]; > */ > > ptr(666); // call it > /* > Implemented as: > if (obj != null) // member function (our case) > (cast(void (*ptr)(Object, int)) ptr.func)(obj, 666); > else // static function > (cast(void (*ptr)(int)) ptr.func)(666); > */ > > ptr = &baz; // store pointer to function baz() > /* > Implemented as: > ptr.obj = null; > ptr.func = &baz; > */ > > ptr(666); // call it > /* > Implemented as: > if (obj != null) // member function > (cast(void (*ptr)(Object, int)) ptr.func)(obj, 666); > else // static function (our case) > (cast(void (*ptr)(int)) ptr.func)(666); > */ > > > As you can see, the check is done at run-time. This makes things a bit > slower, > but more flexible. For example, you could write: > > > // sorts array of strings, using user-defined comparison function > void sort(char[][] s, delegate int cmp(char[], char[])); > > > int my_sort(char[] s1, char[] s2); > sort(foo, &my_sort); > > > class Bar > { > bit case_sensitive; > > int bar_sort(char[] s1, char[] s2) > { > if (this.case_sensitive) > ... > else > ... > } > > void baz() > { > sort(foo, &bar_sort); > } > } > > > |
April 02, 2002 Re: new word: "dommunity" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3CA8C79E.703CAD34@deming-os.org... > Svyatoslav Bezgin wrote: > > > I ask You about it. Not only for me, but for whole Dcommunity.... > > Maybe, since we're moving from C to D, we should be known as a dommunity... :) > > -- > 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))) ] > > But in Dutch 'dom' means stupid... :( -- Stijn OddesE_XYZ@hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail |
April 03, 2002 Re: new word: "dommunity" | ||||
---|---|---|---|---|
| ||||
Posted in reply to OddesE | OddesE wrote:
> "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3CA8C79E.703CAD34@deming-os.org...
> > Svyatoslav Bezgin wrote:
> >
> > > I ask You about it. Not only for me, but for whole Dcommunity....
> >
> > Maybe, since we're moving from C to D, we should be known as a
> dommunity... :)
> >
> > --
> > 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))) ]
> >
> >
>
> But in Dutch 'dom' means stupid... :(
Yeah, but it's not like we want to be called the "Kaas Kopmunity"...
-BobC
|
April 03, 2002 Re: new D site | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | The C# syntax I find very confusing. The delegate declaration looks like a storage class, but it isn't, it's a type component. It doesn't seem possible in C#, because of the way declarations work, to create an array of delegates or a pointer to a delegate or a function returning a delegate. The static/virtual delegate call shouldn't cause a slowdown in D, for the happy coincidence that the 'this' pointer is passed in EAX. Hence, just stuff the object reference in EAX after pushing the parameters. If the function doesn't take a this pointer, it ignores EAX with no adverse consequences. "Pavel Minayev" <evilone@omen.ru> wrote in message news:a8d552$2fc7$1@digitaldaemon.com... > "Walter" <walter@digitalmars.com> wrote in message news:a8d3t1$2em9$1@digitaldaemon.com... > > > Is it expected to work with non-member functions too, with member > functions > > arbitrarilly mixed in? > > It would be just great (and it is the way C# delegates work). Practically, internals could look like this: > > // format of delegate pointer > struct Delegate > { > Object obj; > void* func; > } > > > > class Form { void bar(int n); } > Form foo; > void baz(int n); > delegate void ptr(int); // declare delegate pointer > > ptr = &foo.bar; // store pointer to method bar() of object foo > /* > Implemented as: > ptr.obj = foo; > ptr.func = &foo.classinfo.vtbl[bar]; > */ > > ptr(666); // call it > /* > Implemented as: > if (obj != null) // member function (our case) > (cast(void (*ptr)(Object, int)) ptr.func)(obj, 666); > else // static function > (cast(void (*ptr)(int)) ptr.func)(666); > */ > > ptr = &baz; // store pointer to function baz() > /* > Implemented as: > ptr.obj = null; > ptr.func = &baz; > */ > > ptr(666); // call it > /* > Implemented as: > if (obj != null) // member function > (cast(void (*ptr)(Object, int)) ptr.func)(obj, 666); > else // static function (our case) > (cast(void (*ptr)(int)) ptr.func)(666); > */ > > > As you can see, the check is done at run-time. This makes things a bit > slower, > but more flexible. For example, you could write: > > > // sorts array of strings, using user-defined comparison function > void sort(char[][] s, delegate int cmp(char[], char[])); > > > int my_sort(char[] s1, char[] s2); > sort(foo, &my_sort); > > > class Bar > { > bit case_sensitive; > > int bar_sort(char[] s1, char[] s2) > { > if (this.case_sensitive) > ... > else > ... > } > > void baz() > { > sort(foo, &bar_sort); > } > } > > > |
April 03, 2002 Re: new D site | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:a8eie6$40d$1@digitaldaemon.com... > The C# syntax I find very confusing. The delegate declaration looks like a storage class, but it isn't, it's a type component. It doesn't seem possible > in C#, because of the way declarations work, to create an array of delegates > or a pointer to a delegate or a function returning a delegate. It wasn't actually C# syntax. In C#, "delegate" is actually a keyword which declares type, and not variable: delegate void Foo(int); // Foo is a new type Foo ptr; // declare a pointer of that type So, you can actually have arrays: Foo[] ptr; Still, I like your syntax more. It is more distinct from normal function declaration. > The static/virtual delegate call shouldn't cause a slowdown in D, for the happy coincidence that the 'this' pointer is passed in EAX. Hence, just stuff the object reference in EAX after pushing the parameters. If the function doesn't take a this pointer, it ignores EAX with no adverse consequences. It's implementation detail, I guess. As long as it works, great, and no matter how it is done. I wouldn't mind if it were slower than normal ptr call, but if you can make it the same, well, it's great! By the way, I guess functions with non-D calling conventions cannot be delegated? |
April 03, 2002 Re: new word: "dommunity" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert W. Cunningham | "Robert W. Cunningham" <rwc_2001@yahoo.com> wrote in message news:3CAA7EAF.A410548E@yahoo.com... > OddesE wrote: > > > "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3CA8C79E.703CAD34@deming-os.org... > > > Svyatoslav Bezgin wrote: > > > > > > > I ask You about it. Not only for me, but for whole Dcommunity.... > > > > > > Maybe, since we're moving from C to D, we should be known as a > > dommunity... :) > > > > > > -- > > > 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))) ] > > > > > > > > > > But in Dutch 'dom' means stupid... :( > > Yeah, but it's not like we want to be called the "Kaas Kopmunity"... > > > -BobC > LOL! How did you know? Do you have friends in Holland? :) -- Stijn OddesE_XYZ@hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail |
April 04, 2002 Re: new D site | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:a8evkh$5tf$1@digitaldaemon.com... > "Walter" <walter@digitalmars.com> wrote in message news:a8eie6$40d$1@digitaldaemon.com... > > The C# syntax I find very confusing. The delegate declaration looks like a > > storage class, but it isn't, it's a type component. It doesn't seem > possible > > in C#, because of the way declarations work, to create an array of > delegates > > or a pointer to a delegate or a function returning a delegate. > It wasn't actually C# syntax. In C#, "delegate" is actually a keyword > which declares type, and not variable: > delegate void Foo(int); // Foo is a new type > Foo ptr; // declare a pointer of that type > So, you can actually have arrays: > Foo[] ptr; That completely threw me. If the descriptions would just *say* that it was a wierd form of typedef, it would have made a lot more sense to me. I couldn't figure out if it was declaring a delegate type, a variable of type delegate, or a function to be delegated, hence I couldn't figure out what a delegate was. I looked at several descriptions of it via Google, and they were all a mess. Needless to say, I am not going to do it that way in D! > Still, I like your syntax more. It is more distinct from normal function declaration. Thanks for pointing out the conceptual simplicity of it just being a function pointer bundled with an object reference. I guess I tend to think about programming concepts differently than others, I see complexity where they see simplicity and vice versa <g>. It really is just an enhanced "pointer to function", so it should declare in a manner similar to pointer to function. I thought of using another special character to replace the * in pointer to function, but that got too wierd looking <g>. I considered *., but that looked too confusing with C++ pointers to members. > By the way, I guess functions with non-D calling conventions cannot be delegated? Sure they can be, they just can't be mixed into one delegate type. This works analogously to the type checking on function pointers. |
April 04, 2002 Re: new D site | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:a8ct54$2aja$1@digitaldaemon.com... > "Jan Jezabek" <jezabek@poczta.onet.pl> wrote in message news:3CA9E3ED.3060600@poczta.onet.pl... > > > Isn't it the same as pointers to static member functions (I might be > > getting something wrong)? > > It is the same type of pointers you could see in C++: > > class Foo; > void (Foo::*ptr)(); // pointer to member of Foo > > Foo foo; > foo->*ptr(); // call it > > I don't really see much need in it, however. In my experience (not so long, > but still...), I've never ever used C++ pointers to members. Can anyone give a sample where these might be useful? They're good for implementing some kinds of state machines... usually the object pointer is something simple, such as "this" or "objectwithmethodtocall". Other people may have found some more exotic uses for it. Sean |
Copyright © 1999-2021 by the D Language Foundation