Given an object, how to call an alias to a member function on it?

Quirin Schroll qs.il.paperinik at gmail.com
Thu May 4 11:34:07 UTC 2023


On Wednesday, 3 May 2023 at 11:38:46 UTC, Adam D Ruppe wrote:
> On Tuesday, 2 May 2023 at 13:57:23 UTC, Steven Schveighoffer 
> wrote:
>> Isn't that what `__traits(child)` is for?
>>
>> https://dlang.org/spec/traits.html#child
>
> Yes, `__traits(child, object, method_alias)(args)` is the way 
> to do it.

This doesn’t work, but a slight modification does the trick:

```d
--- a.d
#line 3 "a.d" // for run.dlang.io

struct S
{
     private void f() {}
}
alias Sf = S.f;

--- b.d
#line 12 "b.d" // for run.dlang.io

import a;

void main()
{
     S s;
     __traits(child, s, Sf)(); // error: Error: struct `a.S` 
function `f` is not accessible
     (&__traits(child, s, Sf))(); // ok
}
```

Thanks for making me aware of `__traits(child)`.


More information about the Digitalmars-d-learn mailing list