Thread overview
Assigning a method name to a variable and then calling it with an object
May 24, 2018
aliak
May 24, 2018
Basile B.
May 24, 2018
aliak
May 24, 2018
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
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
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!