How to return a reference to structs?

Ali Çehreli acehreli at yahoo.com
Sat Nov 6 13:57:47 UTC 2021


On 11/6/21 5:05 AM, Andrey Zherikov wrote:

 > Some one can complain that `foo()` returns pointers that are not
 > available in CTFE but remember that the real code is more complex and
 > `foo()` cam be return just length (`return b.get().length;`) having the
 > same result.

I think the error you got is a bug and should be reported because it 
does not make sense:

   Error: couldn't find field `ar` of type `A[]` in `MapResult([0LU], null)`

 > Basically I have a collection of data (`A[] ar`), different addressing
 > through the indexes (multiple `size_t[] idx`) and I want to manipulate
 > these objects through indexes. If there is another way to achieve the
 > same in CFTE, I'd like to hear.

Have you considered std.range.indexed, which should at least cover the 
case of accessing:

   https://dlang.org/phobos/std_range.html#indexed

For changing values, how about passing in a function pointer e.g. a 
lambda? The following one takes A and returns A but you can have a 
different approach where the function takes by 'ref A' and modifies its 
parameter:

   void set(A function(A) func) {
     foreach (i; idx) {
       ar[i] = func(ar[i]);
     }
   }

Can you give a little more context for others to come up with a better 
answer?

Ali



More information about the Digitalmars-d-learn mailing list