September 03, 2015 Why isn't int[] automatically convertible to long[]? | ||||
---|---|---|---|---|
| ||||
pragma(msg, is(int[] : long[])); false Why? |
September 03, 2015 Re: Why isn't int[] automatically convertible to long[]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | 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. |
Copyright © 1999-2021 by the D Language Foundation