This needs to be fixed

Max Samukha maxsamukha at gmail.com
Wed Aug 28 16:37:12 UTC 2024


On Sunday, 25 August 2024 at 19:40:48 UTC, Timon Gehr wrote:
>
> FWIW with my frontend this just works:
>

I've always wished there'd be a way for a nested struct to access 
its parent context. Can your frontend handle something like this:

```
struct S2(alias instance)
{
     alias Outer = __traits(parent, instance);
     ref Outer outer() => *cast(Outer*)(cast(void*)&this - 
instance.offsetof);
}

struct S
{
     S2!inner inner;
}
```
?

There's a workaround using mixins, but it's not great:

```
mixin template S2(string name)
{
     alias Outer = typeof(this);

     struct S2
     {
         ref Outer outer() => *cast(Outer*)(cast(void*)&this - 
field.offsetof);
         void foo()
         {
             import std.stdio;
             writeln(name, ": outer.x = ", outer.x);
         }
     }

     mixin("S2 ", name, ";");
     mixin("alias field = ", name, ";");
}

struct S
{
     int x;
     mixin S2!"inner";
}

void main()
{
     S s = S(42);
     s.inner.foo();
}
```





More information about the Digitalmars-d mailing list