How to return a reference to structs?

Ali Çehreli acehreli at yahoo.com
Sat Nov 6 04:28:05 UTC 2021


On 11/5/21 5:43 PM, Andrey Zherikov wrote:

In case others want to work, here are the modules that need to be imported:

import std.algorithm;
import std.range;
import std.stdio;

 > struct A {}
 > struct B
 > {
 >      A[] ar = [A.init];
 >      size_t[] idx = [0];

I don't know why but those initial values are the reason for the 
problem. It works without them.

 >
 >      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
 > }

Ali



More information about the Digitalmars-d-learn mailing list