casting array literals doesn't work as stated in the docs

Steven Schveighoffer schveiguy at yahoo.com
Sun Jan 3 18:45:18 PST 2010


On Sun, 03 Jan 2010 06:53:53 -0500, Trass3r <un at known.com> wrote:

> Am 03.01.2010, 12:33 Uhr, schrieb bearophile <bearophileHUGS at lycos.com>:
>
>>
>> // alternative 1:
>> enum RGBA[] data1 = [{0x00, 0x10, 0x20, 0x30}, {0x40, 0x50, 0x60,  
>> 0x70}];
>> // alternative 2:
>> enum RGBA[] data2 = [RGBA(0x00, 0x10, 0x20, 0x30), RGBA(0x40, 0x50,  
>> 0x60, 0x70)];
>>
>>
>
> Alternative 1: I heard struct literals could be removed.
> Alternative 2:
> 1) it's cumbersome, especially if you got a huge array
> 2) if you somehow fundamentally change the struct for whatever reason,  
> you probably have to reassign everything.
>
> I currently use the old casting hack:
> auto PALETTE = (cast(RGBA*) cast(ubyte[]) [0xFF,...]) [0 .. 256];

You should be able to do this I would think:

auto PALETTE = cast(RGBA[])(cast(ubyte[])[0xFF,..]));

but it doesn't work.  I think it probably should.  Why would I cast each  
element to byte only to then cast to RGBA.  I think the parentheses should  
make it clear how I want the cast to proceed.  I agree there should be a  
way to invoke the "bitwise" cast in one line besides the hackish way you  
are forced to do it.

-Steve



More information about the Digitalmars-d mailing list