Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
April 22, 2016 "inline" conversion of array to immutable | ||||
---|---|---|---|---|
| ||||
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? |
April 22, 2016 Re: "inline" conversion of array to immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeff Thompson | 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 http://dlang.org/phobos/std_exception.html#.assumeUnique |
April 22, 2016 Re: "inline" conversion of array to immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to FreeSlave | 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:
>> 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 http://dlang.org/phobos/std_exception.html#.assumeUnique
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);
}
|
April 22, 2016 Re: "inline" conversion of array to immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeff Thompson | 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;
}();
}
----
|
April 22, 2016 Re: "inline" conversion of array to immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeff Thompson | 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:
>> 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 http://dlang.org/phobos/std_exception.html#.assumeUnique
>
> 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);
> }
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);
}
|
April 22, 2016 Re: "inline" conversion of array to immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | 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;
}();
}
|
April 22, 2016 Re: "inline" conversion of array to immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeff Thompson | 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.
|
Copyright © 1999-2021 by the D Language Foundation