www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why isn't int[] automatically convertible to long[]?

reply "Jack Stouffer" <jack jackstouffer.com> writes:
     pragma(msg, is(int[] : long[]));

     false


Why?
Sep 03 2015
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Sep 03 2015