Thread overview
static array casting
Mar 27, 2008
Hoenir
Apr 01, 2008
Hoenir
March 27, 2008
Why does

RGBA[] PALETTE = cast(RGBA[]) [
    0x00, 0x00, 0x00, 0x00, 0xE3, 0x53, 0x00, 0x00,...... ];

work, but

RGBA[256] PALETTE = cast(RGBA[256]) [
    0x00, 0x00, 0x00, 0x00, 0xE3, 0x53, 0x00, 0x00,...];

doesn't?
RGBA simply is a struct containing r,g,b and a as ubytes.
March 27, 2008
"Hoenir" <mrmocool@gmx.de> wrote in message news:fsgsed$18s3$1@digitalmars.com...
> Why does
>
> RGBA[] PALETTE = cast(RGBA[]) [
>     0x00, 0x00, 0x00, 0x00, 0xE3, 0x53, 0x00, 0x00,...... ];
>
> work, but
>
> RGBA[256] PALETTE = cast(RGBA[256]) [
>     0x00, 0x00, 0x00, 0x00, 0xE3, 0x53, 0x00, 0x00,...];
>
> doesn't?
> RGBA simply is a struct containing r,g,b and a as ubytes.

Works for me.  Are you sure you have exactly 1024 values in that array?


March 27, 2008
"Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:fsh6bs$23oi$1@digitalmars.com...
> "Hoenir" <mrmocool@gmx.de> wrote in message news:fsgsed$18s3$1@digitalmars.com...
>> Why does
>>
>> RGBA[] PALETTE = cast(RGBA[]) [
>>     0x00, 0x00, 0x00, 0x00, 0xE3, 0x53, 0x00, 0x00,...... ];
>>
>> work, but
>>
>> RGBA[256] PALETTE = cast(RGBA[256]) [
>>     0x00, 0x00, 0x00, 0x00, 0xE3, 0x53, 0x00, 0x00,...];
>>
>> doesn't?
>> RGBA simply is a struct containing r,g,b and a as ubytes.
>
> Works for me.  Are you sure you have exactly 1024 values in that array?

Stupid OE.

You weren't very clear on what what was happening.  It works when it's in a function, but not when declared at global scope.

As for why it fails with a stupid "non-const expression" error?  Who knows. Probably a bug.  Maybe it's already committed.

As a weird workaround, you can do this:

RGBA[256] Pal = (cast(RGBA[])[ ... ])[0 .. 256];


April 01, 2008
Jarrett Billingsley schrieb:
> You weren't very clear on what what was happening.  It works when it's in a function, but not when declared at global scope.
> 
Yes I use it at global scope.

> As for why it fails with a stupid "non-const expression" error?  Who knows. Probably a bug.  Maybe it's already committed.
> 
Yes, the error is "Error: non-constant expression cast(RGBA [256u])"

> As a weird workaround, you can do this:
> 
> RGBA[256] Pal = (cast(RGBA[])[ ... ])[0 .. 256]; 
> 
I already considered using slicing but didn't get it to work. Thank you :)