www.digitalmars.com         C & C++   DMDScript  

D - New to group, quick question/suggestion

reply Jonathan Andrew <jon ece.arizona.edu> writes:
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
Apr 09 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"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?
Apr 09 2002
parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"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
Apr 09 2002
parent reply "Walter" <walter digitalmars.com> writes:
"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.)
Apr 09 2002
next sibling parent "Pavel Minayev" <evilone omen.ru> writes:
"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.
Apr 09 2002
prev sibling parent reply "Sean L. Palmer" <spalmer iname.com> writes:
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.)
Apr 10 2002
parent "Walter" <walter digitalmars.com> writes:
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.)
Apr 11 2002