Thread overview
Method pointers
Oct 18, 2003
Dario
Nov 04, 2003
Walter
Nov 04, 2003
Charles Sanders
Nov 05, 2003
Dario
Nov 05, 2003
Dario
Jan 26, 2004
Walter
October 18, 2003
D delegates are a structure of an object (or a stack frame pointer) and a
pointer to a method (or to a nested function).
Anyway D has no way to declare a pointer to a method: Walter probably thinks
it is useless, or not worthy the effort of implementing it.
I find it a very useful feature instead (since they help me to write less or
simpler code).

Is that so difficult to implement?
What's wrong about a sintax like:
.   class C
.   {    void f1() { fp = &f2; }
.         void f2() { fp = &f1; }
.         void C.function() fp;
.         void f() { this.*fp(); } // we can reuse C++ sintax here
.    }
NOTE: actually you have to use delegates and waste 4 bytes for the reference
and some clock cycles.

I think that D should provide all the basic concepts and let the users code with them: it is more important that D supports function and method pointers than delegates (even if they can be useful sometimes, and would be even more useful if a function pointer could be casted to a delegate!).

(Please forgive me for my English! I understand English well but I find it hard to speak it correctly. ;-)


November 04, 2003
I recognize that pointers to members are useful, but I think that delegates are conceptually more powerful and cover the uses for pointers to members. The more I use delegates the more useful they get! Anyhow, I ask that you give delegates another chance.

"Dario" <supdar@yahoo.com> wrote in message news:bmrc5d$16c3$1@digitaldaemon.com...
> D delegates are a structure of an object (or a stack frame pointer) and a
> pointer to a method (or to a nested function).
> Anyway D has no way to declare a pointer to a method: Walter probably
thinks
> it is useless, or not worthy the effort of implementing it.
> I find it a very useful feature instead (since they help me to write less
or
> simpler code).
>
> Is that so difficult to implement?
> What's wrong about a sintax like:
> .   class C
> .   {    void f1() { fp = &f2; }
> .         void f2() { fp = &f1; }
> .         void C.function() fp;
> .         void f() { this.*fp(); } // we can reuse C++ sintax here
> .    }
> NOTE: actually you have to use delegates and waste 4 bytes for the
reference
> and some clock cycles.
>
> I think that D should provide all the basic concepts and let the users
code
> with them: it is more important that D supports function and method
pointers
> than delegates (even if they can be useful sometimes, and would be even
more
> useful if a function pointer could be casted to a delegate!).
>
> (Please forgive me for my English! I understand English well but I find it hard to speak it correctly. ;-)
>
>


November 04, 2003
delegates rock!

consider the following two snippets of code:

// C++

class MsgSlot {
public:
 virtual bool proccess( LRESULT &rv, WPARAM wparam, LPARAM lparam ) { return
false; }

};


typedef LRESULT (MsgSlot::*win32Func) (LRESULT&, WPARAM, LPARAM);
typedef LRESULT (MsgSlot::*mouseFunc) (int,int,int);


class Win32MsgSlot : public MsgSlot {
public:
 Win32MsgSlot(win32Func func) : _func(func) { }

 virtual bool proccess(LRESULT &rv, WPARAM wparam, LPARAM lparam ) {
  (*this.*_func) (rv,wparam,lparam );
  return true;
 }

protected:

 win32Func _func;
};


// D


class MsgSlot
{
public:
 bit processMessage( out LRESULT rv, WPARAM wparam, LPARAM lparam )
 {
  return false;
 }
}

alias LRESULT delegate(WPARAM,LPARAM) win32Func;
alias LRESULT delegate(int,int,int) mouseFunc;

class Win32MsgSlot : MsgSlot
{
protected:
 win32Func _func;
public:
 this( win32Func func )
 {
  _func = func;
 }

 bit processMessage( out LRESULT rv, WPARAM wparam, LPARAM lparam )
 {
  rv = _func( wparam, lparam );
  return true;
 }
}


Much cleaner!

C


"Walter" <walter@digitalmars.com> wrote in message news:bo703s$2uji$1@digitaldaemon.com...
> I recognize that pointers to members are useful, but I think that
delegates
> are conceptually more powerful and cover the uses for pointers to members. The more I use delegates the more useful they get! Anyhow, I ask that you give delegates another chance.
>
> "Dario" <supdar@yahoo.com> wrote in message news:bmrc5d$16c3$1@digitaldaemon.com...
> > D delegates are a structure of an object (or a stack frame pointer) and
a
> > pointer to a method (or to a nested function).
> > Anyway D has no way to declare a pointer to a method: Walter probably
> thinks
> > it is useless, or not worthy the effort of implementing it.
> > I find it a very useful feature instead (since they help me to write
less
> or
> > simpler code).
> >
> > Is that so difficult to implement?
> > What's wrong about a sintax like:
> > .   class C
> > .   {    void f1() { fp = &f2; }
> > .         void f2() { fp = &f1; }
> > .         void C.function() fp;
> > .         void f() { this.*fp(); } // we can reuse C++ sintax here
> > .    }
> > NOTE: actually you have to use delegates and waste 4 bytes for the
> reference
> > and some clock cycles.
> >
> > I think that D should provide all the basic concepts and let the users
> code
> > with them: it is more important that D supports function and method
> pointers
> > than delegates (even if they can be useful sometimes, and would be even
> more
> > useful if a function pointer could be casted to a delegate!).
> >
> > (Please forgive me for my English! I understand English well but I find
it
> > hard to speak it correctly. ;-)
> >
> >
>
>


November 05, 2003
Walter:
>I recognize that pointers to members are useful, but I think that delegates are conceptually more powerful and cover the uses for pointers to members. The more I use delegates the more useful they get! Anyhow, I ask that you give delegates another chance.

I'm not saying delegates are bad.
But method pointers are useful and not
always can they be replaced by delegates.

Anyway, what's wrong with method pointers?
Are they just hard to implement?

(I just want to remember that D is meant not to
be difficult for C++ coders to switch to.
Is this unimportant now? Don't you think that
their absence would cause problems when converting
C++ code into D?)

P.S.= Perhaps I'd better to explain what I use
method pointers for. Consider that I want to
change a class behaviour according to its state.
I'd like write the following code:
  Class C
  {
      void C.function() fp;
      void func1() {...}
      void func2() {...}
      void func()
      {   ...
          ...
          this.fp();
          ...
      }
      void change() { fp = (fp!=&func1)?&func1:&func2; }
  }
If I used delegates I would be wasting 4 bytes
for the pointer to this (which is part of the
delegate).
In some cases I don't even know which object I
will use the method with, so I can't use delegates
at all.

Note that if we want method pointers to be
polymorphic, they have to be index for the vtables...
But this is not as important.

Thanks for your attention.
Dario


November 05, 2003
Charles Sanders:
>delegates rock!
>consider the following two snippets of code:
>[...]
>Much cleaner!

Yes, I agree this is a case where delegates
are the best option. I like that D has them,
but I regret that a more basic feature like
method pointers is missing. :(


January 26, 2004
"Dario" <Dario_member@pathlink.com> wrote in message news:bob2k0$2lbv$1@digitaldaemon.com...
> Walter:
> >I recognize that pointers to members are useful, but I think that
delegates
> >are conceptually more powerful and cover the uses for pointers to
members.
> >The more I use delegates the more useful they get! Anyhow, I ask that you give delegates another chance.
>
> I'm not saying delegates are bad.
> But method pointers are useful and not
> always can they be replaced by delegates.
>
> Anyway, what's wrong with method pointers?
> Are they just hard to implement?

They are hard to implement, and hard to understand.

> (I just want to remember that D is meant not to
> be difficult for C++ coders to switch to.
> Is this unimportant now? Don't you think that
> their absence would cause problems when converting
> C++ code into D?)
>
> P.S.= Perhaps I'd better to explain what I use
> method pointers for. Consider that I want to
> change a class behaviour according to its state.
> I'd like write the following code:
>   Class C
>   {
>       void C.function() fp;
>       void func1() {...}
>       void func2() {...}
>       void func()
>       {   ...
>           ...
>           this.fp();
>           ...
>       }
>       void change() { fp = (fp!=&func1)?&func1:&func2; }
>   }
> If I used delegates I would be wasting 4 bytes
> for the pointer to this (which is part of the
> delegate).
> In some cases I don't even know which object I
> will use the method with, so I can't use delegates
> at all.

What you can do is add a method that combines the object with the method into a delegate, and returns that when the code does know what the object is.

> Note that if we want method pointers to be
> polymorphic, they have to be index for the vtables...
> But this is not as important.
>
> Thanks for your attention.
> Dario
>
>