Implicit cast to immutable

Daniel Murphy yebblies at nospamgmail.com
Sat Oct 15 20:05:43 PDT 2011


The implicit conversion to immutable is only possible inside strongly pure 
functions.  When the parameter is 'in int[]' foo cannot be strongly pure, 
only const pure.  As 'in int[2]' is a value type, the second foo can be 
strongly pure.

'new' expressions will hopefully be able to be converted to immutable 
eventually, along with array concatenation and array.dup.

It is also likely that the following will be valid code (const pure foo 
called with immutable arguments)

int[] foo(in int[] x) pure {
   return new int[1];
}

void main() {
  immutable x = foo([1, 2, 3].idup);
}

"bearophile" <bearophileHUGS at lycos.com> wrote in message 
news:j6iom9$2g1m$1 at digitalmars.com...
> Do you know why this program doesn't compile (with DMD 2.056head)?
>
>
> immutable(int[]) foo(in int[] x) pure {
>    return new int[1];
> }
> void main() {}
>
>
> It gives:
> test.d(2): Error: cannot implicitly convert expression (new int[](1u)) of 
> type int[] to immutable(int[])
>
> This program instead compiles (now x is int[2] instead of int[]):
>
> immutable(int[]) foo(in int[2] x) pure {
>    return new int[1];
> }
> void main() {}
>
>
> Bye and thank you,
> bearophile 




More information about the Digitalmars-d-learn mailing list