www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static array casting

reply Hoenir <mrmocool gmx.de> writes:
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.
Mar 27 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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?
Mar 27 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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];
Mar 27 2008
parent Hoenir <mrmocool gmx.de> writes:
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 :)
Apr 01 2008