Thread overview |
---|
May 24, 2018 Assigning a method name to a variable and then calling it with an object | ||||
---|---|---|---|---|
| ||||
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 Re: Assigning a method name to a variable and then calling it with an object | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | 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 Re: Assigning a method name to a variable and then calling it with an object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | 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!
|
Copyright © 1999-2021 by the D Language Foundation