"inline" conversion of array to immutable
ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Apr 22 04:16:59 PDT 2016
On 22.04.2016 13:07, Jeff Thompson wrote:
> OK, we lose the compiler check for correctness. What if I put func
> directly in main with the hopes that the compiler will check correctness
> and also inline the function? But it won't assign to the immutable
> array. Why not? It's the same function.
>
> void main(string[] args) {
> int[] func(int x) pure {
> int[] result = new int[10];
> result[0] = x;
> return result;
> }
> immutable int[] array = func(1);
> }
It's a nested function now. That means, it could reference local
variables of main. Make func static and it works.
You can also use a function literal:
----
void main()
{
immutable int[] array = {
int[] result = new int[10];
result[0] = 1;
return result;
}();
}
----
More information about the Digitalmars-d-learn
mailing list