Assigning a method name to a variable and then calling it with an object

aliak something at something.com
Thu May 24 23:17:19 UTC 2018


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!





More information about the Digitalmars-d-learn mailing list