www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "inline" conversion of array to immutable

reply Jeff Thompson <nospam example.com> writes:
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
parent reply FreeSlave <freeslave93 gmail.com> writes:
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
Apr 22 2016
parent reply Jeff Thompson <nospam example.com> writes:
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); }
Apr 22 2016
next sibling parent reply ag0aep6g <anonymous example.com> writes:
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
parent reply Jeff Thompson <nospam example.com> writes:
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
parent ag0aep6g <anonymous example.com> writes:
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
prev sibling parent FreeSlave <freeslave93 gmail.com> writes:
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); }
Apr 22 2016