Safe cast of arrays

Iakh via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 10 03:51:41 PST 2016


On Wednesday, 10 February 2016 at 08:49:21 UTC, w0rp wrote:
> On Tuesday, 9 February 2016 at 21:20:53 UTC, Iakh wrote:
>> https://dlang.org/spec/function.html#function-safety
>> Current definition of safety doesn't mention cast of arrays.
>
> I think this should be addressed, as if you can't cast between 
> pointer types, you shouldn't be allowed to cast between slice 
> types either. Because slices are just a pointer plus a length. 
> Another way to demonstrate the problem is like this.

If address it in same fashion as it's done for other things:

  4. Cannot access unions that have pointers or references
     overlapping with other types.

"reinterpret cast" is allowed using unions but only for some
types. So array casting could be allowed for same set of types.
e.g cast(T)arrB allowed if below compiles:
() @safe {
   union
   {
     T a;
     typeof(arraB[0]) b;
   }
}
Is the condition sufficient?
Does this change needs DIP?


More information about the Digitalmars-d mailing list