## digitalmars.D.learn - How to init immutable static array?

- Miguel L (12/12) Jul 18 2017 Hi, I need help again. I have an immutable static array and i
- Era Scarecrow (16/26) Jul 18 2017 I'd probably separate the calculations into a separate function,
- Era Scarecrow (2/3) Jul 18 2017 i meant: tmp[i]=some calculations(based on constants and n)
- Miguel L (2/5) Jul 18 2017 That does work, thanks.

Hi, I need help again. I have an immutable static array and i need to initialize its contents inside a for loop. Something like this: void f(int n)(....) { immutable float[n] my_array; for(int i=0;i<n;++i) my_array[n]=some calculations(based on constants and n) ... } What is the better way to achieve this? Thanks in advance.

Jul 18 2017

On Tuesday, 18 July 2017 at 07:20:48 UTC, Miguel L wrote:Hi, I need help again. I have an immutable static array and i need to initialize its contents inside a for loop. Something like this: void f(int n)(....) { immutable float[n] my_array; for(int i=0;i<n;++i) my_array[n]=some calculations(based on constants and n) ... }I'd probably separate the calculations into a separate function, and assign the immutable data all at once (which you have to do similarly with constructors). So... i think this would work... void f(int n)(....) { auto static calculate() { float[n] tmp; for(int i=0;i<n;++i) my_array[i]=some calculations(based on constants and n) ... return cast(immutable) tmp; } immutable float[n] my_array = calculate(); ... }

Jul 18 2017

On Tuesday, 18 July 2017 at 07:30:30 UTC, Era Scarecrow wrote:my_array[i]=some calculations(based on constants and n)i meant: tmp[i]=some calculations(based on constants and n)

Jul 18 2017

On Tuesday, 18 July 2017 at 07:31:22 UTC, Era Scarecrow wrote:On Tuesday, 18 July 2017 at 07:30:30 UTC, Era Scarecrow wrote:That does work, thanks.my_array[i]=some calculations(based on constants and n)i meant: tmp[i]=some calculations(based on constants and n)

Jul 18 2017