digitalmars.D.learn - Bind object to member function
- Li Jie (7/7) Aug 13 2006 class Test{
- Derek Parnell (7/16) Aug 13 2006 I do not know what 'bind' means in this context. Is it a Java term? Why ...
- Kirk McDonald (18/27) Aug 13 2006 You want pointers to member functions, as C++ has. D does not have
- Li Jie (4/4) Aug 13 2006 If you're looking for pointers to member functions to implement...
- Kirk McDonald (16/21) Aug 14 2006 My own Pyd library requires this functionality to implement its class
- BCS (26/35) Aug 14 2006 something like this works
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
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
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
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
Li Jie wrote: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/wikiIf 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 14 2006
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