www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Assigning a method name to a variable and then calling it with an

reply aliak <something something.com> writes:
Hi,

I was essentially trying to do this:

struct S {
   void f() {}
}

auto f = S.f; // f becomes void function(S) ??
S s;
f(s);

Is something like that possible?

Cheers,
- Ali
May 24 2018
parent reply Basile B. <b2.temp gmx.com> writes:
On Thursday, 24 May 2018 at 23:03:21 UTC, aliak wrote:
 Hi,

 I was essentially trying to do this:

 struct S {
   void f() {}
 }

 auto f = S.f; // f becomes void function(S) ??
 S s;
 f(s);

 Is something like that possible?

 Cheers,
 - Ali
Sure: ``` import std.stdio; void main(string[] args) { struct S { void f() {"yeah possible".writeln;} } void delegate() f; f.funcptr = &S.f; S s; f.ptr = &s; s.f(); } ``` It's just that you have to learn the ABI of D delegates. There are two members: .funcptr (function) and .ptr (context, i.e the "this").
May 24 2018
parent aliak <something something.com> writes:
On Thursday, 24 May 2018 at 23:08:29 UTC, Basile B. wrote:
 On Thursday, 24 May 2018 at 23:03:21 UTC, aliak wrote:
 Hi,

 I was essentially trying to do this:

 struct S {
   void f() {}
 }

 auto f = S.f; // f becomes void function(S) ??
 S s;
 f(s);

 Is something like that possible?

 Cheers,
 - Ali
Sure: ``` import std.stdio; void main(string[] args) { struct S { void f() {"yeah possible".writeln;} } void delegate() f; f.funcptr = &S.f; S s; f.ptr = &s; s.f(); } ``` It's just that you have to learn the ABI of D delegates. There are two members: .funcptr (function) and .ptr (context, i.e the "this").
ahh, gracias!
May 24 2018