www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Bind object to member function

reply Li Jie <cpunion gmail.com> writes:
class Test{
  void a(int n){}
}

auto a = &Test.a;
auto t = new Test;
a(t, 1); // or: a.bind(t); a(1);

Is this supported?
Aug 13 2006
next sibling parent Derek Parnell <derek psyc.ward> writes:
On Sun, 13 Aug 2006 20:32:25 +0000 (UTC), Li Jie wrote:

 class Test{
   void a(int n){}
 }
 
 auto a = &Test.a;
 auto t = new Test;
 a(t, 1); // or: a.bind(t); a(1);
 
 Is this supported?
I do not know what 'bind' means in this context. Is it a Java term? Why is it needed? -- Derek Parnell Melbourne, Australia "Down with mediocrity!"
Aug 13 2006
prev sibling next sibling parent Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Li Jie wrote:
 class Test{
   void a(int n){}
 }
 
 auto a = &Test.a;
 auto t = new Test;
 a(t, 1); // or: a.bind(t); a(1);
 
 Is this supported?
You want pointers to member functions, as C++ has. D does not have these. Rather, it has delegates, which are often more useful: class Test { void a (int n) {} } Test t = new Test; void delegate(int) dg = &t.a; dg(1); This use of delegates is documented here: http://www.digitalmars.com/d/function.html#closures If you're looking for pointers to member functions to implement some sort of dispatch mechanism, delegates do not directly allow this. However, there do exist hacks to allow it. -- Kirk McDonald Pyd: Wrapping Python with D http://dsource.org/projects/pyd/wiki
Aug 13 2006
prev sibling next sibling parent reply Li Jie <cpunion gmail.com> writes:

If you're looking for pointers to member functions to implement some
sort of dispatch mechanism, delegates do not directly allow this.

Thanks.
Delegate is very cool, but it bind too early.
Aug 13 2006
parent Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Li Jie wrote:
 If you're looking for pointers to member functions to implement some
 sort of dispatch mechanism, delegates do not directly allow this.
Thanks. Delegate is very cool, but it bind too early.
My own Pyd library requires this functionality to implement its class wrapping. I do not endorse this practice, but the dirty hack it uses to get the functionality is found here: http://dsource.org/projects/pyd/browser/trunk/infrastructure/pyd/dg_convert.d In essense, it constructs a delegate from an Object instance and a regular function pointer. It takes advantage of how delegates are represented internally, but this behavior is not guaranteed by the spec. It is therefore firmly in the realm of "dirty hacks." Note that this module requires the ftype module originally written by Daniel Keep. The version of ftype in Pyd is slightly modified from his original. -- Kirk McDonald Pyd: Wrapping Python with D http://dsource.org/projects/pyd/wiki
Aug 14 2006
prev sibling parent BCS <BCS pathlink.com> writes:
Li Jie wrote:
 class Test{
   void a(int n){}
 }
 
 auto a = &Test.a;
 auto t = new Test;
 a(t, 1); // or: a.bind(t); a(1);
 
 Is this supported?
something like this works #auto b = function(Test t) #auto t = new Test; #b(t); Add parameters to the function if a has parameters. Also say Test has a "real c(char[],int,int)" and you don't want to set all of it's parameters, you can do this: #int i,j; #auto d = (Test test, char[] ch) #auto ret = d(t,"foo");
Aug 14 2006