On Saturday, 7 September 2013 at 23:05:31 UTC, Peter Alexander
wrote:
On Saturday, 7 September 2013 at 22:57:40 UTC, Xinok wrote:
immutable
{
int[] arr = ...;
int[] arr2 = arr.dup;
sort(arr); // arr is still mutable
int[] arr2 = arr.dup;
reverse(arr2);
arr ~= arr2;
}
How do you stop mutable references escaping in functions called
from the immutable block?
e.g.
int* p;
void foo(ref int x) { p = &x; }
void main()
{
immutable
{
int x = 1;
foo(x);
}
*p = 0;
}
Immutable blocks would have the same restrictions as pure
functions. And like pure functions, you could only call other
pure functions. Since foo is not pure, this code wouldn't compile.