digitalmars.D.learn - "inline" conversion of array to immutable
- Jeff Thompson (25/25) Apr 22 2016 Hello. The following code compiles OK where func creates a
- FreeSlave (3/6) Apr 22 2016 Probably this is what you look for
- Jeff Thompson (13/20) Apr 22 2016 OK, we lose the compiler check for correctness. What if I put
- ag0aep6g (14/26) Apr 22 2016 It's a nested function now. That means, it could reference local
- Jeff Thompson (10/22) Apr 22 2016 Great! Making it static works. The function literal also works if
- ag0aep6g (5/14) Apr 22 2016 I take it you're on 2.070 or older then. 2.071 accepts it without
- FreeSlave (10/31) Apr 22 2016 Not sure why, but making func static fixes this:
Hello. The following code compiles OK where func creates a mutable array and main assigns it to an immutable variable: int[] func(int x) pure { int[] result = new int[10]; result[0] = x; return result; } void main(string[] args) { immutable int[] array = func(1); } I assume this works because func is pure so that the compiler knows that its return value won't be changed by some other code. But it doesn't compile when I put the same code from func "inline" into main: void main(string[] args) { int[] result = new int[10]; result[0] = 1; immutable int[] array = result; } I could forcibly cast result to immutable int[], but that seems error-prone. How to tell the compiler that result will not be changed after assigning to array so that the immutable assignment compiles?
Apr 22 2016
On Friday, 22 April 2016 at 09:25:32 UTC, Jeff Thompson wrote:Hello. The following code compiles OK where func creates a mutable array and main assigns it to an immutable variable: [...]Probably this is what you look for
Apr 22 2016
On Friday, 22 April 2016 at 09:40:14 UTC, FreeSlave wrote:On Friday, 22 April 2016 at 09:25:32 UTC, 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); }Hello. The following code compiles OK where func creates a mutable array and main assigns it to an immutable variable: [...]Probably this is what you look for
Apr 22 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; }(); } ----
Apr 22 2016
On Friday, 22 April 2016 at 11:16:59 UTC, ag0aep6g wrote: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; }(); }Great! Making it static works. The function literal also works if I add "function int[]()": void main(string[] args) { immutable int[] array = function int[]() { int[] result = new int[10]; result[0] = 1; return result; }(); }
Apr 22 2016
On 22.04.2016 13:46, Jeff Thompson wrote:The function literal also works if I add "function int[]()": void main(string[] args) { immutable int[] array = function int[]() { int[] result = new int[10]; result[0] = 1; return result; }(); }I take it you're on 2.070 or older then. 2.071 accepts it without `function`, which seems a bit inconsistent when I think about it. Anyway, just `function` should be enough. But of course you can specify return type and empty parameter list if you want.
Apr 22 2016
On Friday, 22 April 2016 at 11:07:47 UTC, Jeff Thompson wrote:On Friday, 22 April 2016 at 09:40:14 UTC, FreeSlave wrote:Not sure why, but making func static fixes this: void main(string[] args) { static int[] func(int x) pure { int[] result = new int[10]; result[0] = x; return result; } immutable int[] array = func(1); }On Friday, 22 April 2016 at 09:25:32 UTC, 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); }Hello. The following code compiles OK where func creates a mutable array and main assigns it to an immutable variable: [...]Probably this is what you look for
Apr 22 2016