Why isn't int[] automatically convertible to long[]?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 3 10:29:49 PDT 2015


On Thursday, 3 September 2015 at 17:27:03 UTC, Jack Stouffer 
wrote:
>     pragma(msg, is(int[] : long[]));
>
>     false
> Why?

Think of the memory layout... if you implicitly casted, either 
the contents would change or it would need to allocate a new 
array, neither of which is free.

[0, 1] as int[] in memory is like [0,0,0,0,0,0,0,1].

[0, 1] as long[] in memory is like 
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1].


It is twice as long! If you casted without the reallocation, 
[0,1] as int[] becomes just plain [1] as long[] - the length 
changes as well as the contents.


More information about the Digitalmars-d-learn mailing list