www.digitalmars.com         C & C++   DMDScript  

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

reply Miguel L <mlabayru gmail.com> writes:
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
parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
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
parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
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
parent Miguel L <mlabayru gmail.com> writes:
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:
       my_array[i]=some calculations(based on constants and n)
i meant: tmp[i]=some calculations(based on constants and n)
That does work, thanks.
Jul 18