How to return a reference to structs?

Andrey Zherikov andrey.zherikov at gmail.com
Sat Nov 6 00:43:55 UTC 2021


On Friday, 5 November 2021 at 20:13:02 UTC, Andrey Zherikov wrote:
> On Friday, 5 November 2021 at 19:49:50 UTC, Ali Çehreli wrote:
>> Indexes are typed as size_t, which is ulong for 64-bit and 
>> uint for 32-bit. If idx elements were indeed indexes, it 
>> should have been typed as size_t:
>> 
>> size_t[] idx = [1,3,4];  // <-- Here
>
>
> Ah, good point. Thanks!

Adding a bit of CTFE:
```d
struct A {}
struct B
{
     A[] ar = [A.init];
     size_t[] idx = [0];

     A*[] get()
     {
         return idx.map!((idx) => &ar[idx]).array;  // Error: 
couldn't find field `ar` of type `A[]` in `MapResult([0LU], null)`
     }
}

auto foo()
{
     B b;
     return b.get();
}

void main()
{
     writeln(foo());       // This works
     pragma(msg, foo());   // This doesn't work
}
```

How to make this work?


More information about the Digitalmars-d-learn mailing list