casting array literals doesn't work as stated in the docs
Denis Koroskin
2korden at gmail.com
Sat Jan 2 17:54:30 PST 2010
On Sun, 03 Jan 2010 04:23:00 +0300, Trass3r <mrmocool at gmx.de> wrote:
> http://www.digitalmars.com/d/2.0/expression.html#ArrayLiteral
>
> The code example from the docs yields [1 1] and [1 0 1 0] instead of [1
> 1] and [257] for dmd 2.037.
>
>
> Additionally
>
> short[] t = [cast(short) 5, 3];
> short[] t2 = [cast(short) 5, 3].dup;
>
> void main()
> { ...
>
> yields:
>
> Error: cannot evaluate _adDupT((& D12TypeInfo_G2i6__initZ),[5,3]) at
> compile-time
> Error: cannot implicitly convert expression (_adDupT((&
> D12TypeInfo_G2i6__initZ),[5,3])) of type int[] to short[]
> Error: cannot evaluate _adDupT((& D12TypeInfo_G2i6__initZ),[5,3]) at
> compile-time
>
>
> Using int instead of short only removes the second message.
>
>
> Compiler bug(s)?
Yes and no. It used to work (and should still work), but the behavior was
changed to take all array literal values into consideration.
"Proper" code should look like this:
short[] t = [cast(short) 5, cast(short)3];
which is err... I'll let someone else to decide. Just imagine there are
ten (or more) values in an array literal.
More information about the Digitalmars-d
mailing list