Implicit cast to immutable
Steven Schveighoffer
schveiguy at yahoo.com
Thu Oct 6 05:59:52 PDT 2011
On Wed, 05 Oct 2011 19:19:37 -0400, bearophile <bearophileHUGS at lycos.com>
wrote:
> 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() {}
I think it's a bug.
new should be considered pure, and since it's return value cannot be a
reference to any parameter, it should implicitly cast to immutable.
The fact that changing the parameter to foo makes it compile is a big clue.
Note, this should compile even if foo isn't pure, since new is pure (no
matter what type of function it is in).
-Steve
More information about the Digitalmars-d-learn
mailing list