Implicit cast to immutable

Christophe travert at phare.normalesup.org
Thu Oct 6 05:08:57 PDT 2011


bearophile , dans le message (digitalmars.D.learn:29961), a écrit :
> Andrej Mitrovic:
> 
>> Maybe:
>> 
>> immutable(int[]) foo(in int[] x) pure {
>>    return new immutable(int[1]);
>> }
>> 
>> void main() {}
> 
> I'd like to know why the code in my original post doesn't compile. I suspect it's a DMD bug, but I am not sure.

The error message tells you why. new int[1] is not castable to immutable 
int[] (in a pure function). The solution is to change new int[1] to make 
it immutable directly. That is very consistent, so I don't think this 
should be considered as a bug. There may be an improvement to ask to 
make the compiler able to check when the cast to immutable is safe, but 
I don't think there is a bug.

>> Or does this have something to do with implicit casts to immutable for
>> pure functions?
> 
> Right.
> 
> 
>> I'm only vaguely familiar with pure..
> 
> I suggest you to use purity more and more in D, because it helps and 
> with the recent bug fixes it is also becoming usable in D (but there 
> are some significant problems left, example: map/filter are not pure 
> yet).

You would need to have pure delegates to have a real effect, wouldn't 
you ?

-- 
Christophe


More information about the Digitalmars-d-learn mailing list