Safe cast of arrays

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Feb 12 05:45:54 PST 2016


On 2/10/16 11:01 PM, Chris Wright wrote:
> On Wed, 10 Feb 2016 22:39:20 -0500, Steven Schveighoffer wrote:
>
>> I think casting a mutable array to any array type is a recipe for memory
>> issues, no matter what is in the elements. Remember that you are casting
>> a reference that still has a mutable pointer to it.
>>
>> @safe should start from a very cautious and overtightened state, and
>> then we loosen it as we find issues.
>>
>> As it was done, it has holes, and so when we fix things, code breaks.
>>
>
> I agree with the principle, but it's always safe to read a pointer as if
> it were not a pointer, and that's what a cast to an immutable array would
> do.
>

A cast to immutable is a guarantee to the compiler that there is no 
other mutable references that you will use again.

This is not the case here.

A cast to const may be viable. However, I think casting in safe code is 
probably not something to allow. If you need to make something work 
outside the compiler's comfort zone, there's always @trusted.

-Steve



More information about the Digitalmars-d mailing list