Idea: Immutable blocks within functions

Xinok xinok at live.com
Sat Sep 7 16:30:59 PDT 2013


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.


More information about the Digitalmars-d mailing list