Thread overview
How to init immutable static array?
Jul 18, 2017
Miguel L
Jul 18, 2017
Era Scarecrow
Jul 18, 2017
Era Scarecrow
Jul 18, 2017
Miguel L
July 18, 2017
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.
July 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();
...
}


July 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)
July 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:
>>       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.