Flatten a range of static arrays

nullptr null at p.tr
Fri Feb 7 20:55:14 UTC 2020


On Friday, 7 February 2020 at 20:13:57 UTC, Dennis wrote:
> If I have an input range with element type `int[3]`, how do I 
> easily turn it into a range of `int` so I can map it?
> If it were an int[3][] I could simply cast it to an int[] 
> before mapping, but I don't want to eagerly turn it into an 
> array.
> I thought of doing this:
> ```
> range.map!(x => x[]).joiner.map!(x => x*2);
> ```
>
> But it gives:
> Error: returning x[] escapes a reference to parameter x, 
> perhaps annotate with return
>
> I tried doing:
> ```
> map!((return x) => x[]) // same error
> map!((return ref x) => x[]) // does not match, map.front is not 
> an lvalue
> ```
>
> I can easily work around it with some more code, but I wonder 
> if someone knows an easy solution.

Depending on how your range is structured, it might be possible 
to just mark front as returning by ref to make this work.


More information about the Digitalmars-d-learn mailing list