Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
April 09, 2002 New to group, quick question/suggestion | ||||
---|---|---|---|---|
| ||||
Just as I was getting fed up with C and C++, what does a random glance at Dr. Dobbs reveal? :) I love the ideas so far, I just have one quick question. I'm not too familiar with the whole concept of delegates, but would it be possible to declare these as part of a struct and call them like a class method? I might not have any idea what I'm talking about, but what I am thinking is as follows: struct foo { int a; int b; void delegate(inout int a, inout int b) swap; swap = &numswap(inout int, inout int); } void numswap(inout int x, inout int y) { int tmp; tmp = x; x = y; y = tmp; } Then you could call foo.swap(), which would in turn call numswap(), and switch the contents of a and b. I realize the syntax for declaring and initializing delegates/function pointers are still up in the air, I tried to make sense of it as best I could and present them here in a reasonable manner. Perhaps what I am really looking for is a way to make classes/objects as simple as structs, with the added bonus that delegates/function pointers can be part of the struct. i.e. (note that I might be taking some liberties with the delegate initialization ideas) class bar { int a; int b; void delegate(intout int a, inout int b) swap = &numswap(inout int, inout int); } void numswap(intout int x, inout int y) { int tmp; tmp = x; x = y; y = tmp; } This could be called with bar.swap() like any other function method. Having the meat of the class methods outside of the actual class declaration seems a little bizarre to me, and I'm sure somebody could come up with a much better way to do this. Basically, by making all class methods just pointers to functions, could structs be eliminated without sacrificing the usefullness of having a lightweight data structure? Maybe come up with an all-in-one data container that allows both easy object-oriented programming and also is familiar to C programmers who just need to use a simple struct. -Jonathan Andrew |
April 09, 2002 Re: New to group, quick question/suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Andrew | "Jonathan Andrew" <jon@ece.arizona.edu> wrote in message news:3CB33E18.494436C7@ece.arizona.edu... > I love the ideas so far, I just have one quick question. I'm not too > familiar with the whole concept > of delegates, but would it be possible to declare these as part of a > struct and call them like a class > method? I might not have any idea what I'm talking about, but what I am > thinking is as follows: > > struct foo > { > int a; > int b; > void delegate(inout int a, inout int b) swap; > swap = &numswap(inout int, inout int); > } > > void numswap(inout int x, inout int y) > { > int tmp; > tmp = x; > x = y; > y = tmp; > } > > Then you could call foo.swap(), which would in turn call numswap(), and > switch the contents of a and b. No, it doesn't work so. Wouldn't it be simplier to just declare swap() as member of struct? |
April 09, 2002 Re: New to group, quick question/suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:a8vh40$18kk$1@digitaldaemon.com... > <SNIP> > > Wouldn't it be simplier to just declare swap() as member of struct? > Is that possible in D? -- Stijn OddesE_XYZ@hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail |
April 09, 2002 Re: New to group, quick question/suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to OddesE | "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:a8voqr$1sc8$1@digitaldaemon.com... > "Pavel Minayev" <evilone@omen.ru> wrote in message news:a8vh40$18kk$1@digitaldaemon.com... > > Wouldn't it be simplier to just declare swap() as member of struct? > Is that possible in D? It is now <g>. (I added support for member functions for struct's.) |
April 10, 2002 Re: New to group, quick question/suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:a8vs2l$25r3$1@digitaldaemon.com... > > Is that possible in D? > > It is now <g>. (I added support for member functions for struct's.) Oh, yes. One thing I really like in Walter is that he silently does all those features that have been requested for a long time, without saying anything, and lets us find them ourselves. =) Just look at the source of dateparse.d... BTW, the best way to find new D features in fresh release is to sort files in src/phobos by date, and check all the newest. |
April 10, 2002 Re: New to group, quick question/suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Awesome! Non-virtual, of course? Sean "Walter" <walter@digitalmars.com> wrote in message news:a8vs2l$25r3$1@digitaldaemon.com... > > It is now <g>. (I added support for member functions for struct's.) |
April 12, 2002 Re: New to group, quick question/suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | Of course. "Sean L. Palmer" <spalmer@iname.com> wrote in message news:a90vfa$174e$1@digitaldaemon.com... > Awesome! > > Non-virtual, of course? > > Sean > > "Walter" <walter@digitalmars.com> wrote in message news:a8vs2l$25r3$1@digitaldaemon.com... > > > > It is now <g>. (I added support for member functions for struct's.) > > > |
Copyright © 1999-2021 by the D Language Foundation