How to init immutable static array?

Era Scarecrow via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 18 00:30:30 PDT 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();
...
}




More information about the Digitalmars-d-learn mailing list