Re: Alias on an array element
On Friday, 13 October 2017 at 13:22:42 UTC, Adam D. Ruppe wrote: * Use a @property ref function to return the array element and trust the compiler to inline it. You could also use pragma(inline, true).
Re: Alias on an array element
On Friday, 13 October 2017 at 01:09:56 UTC, solidstate1991 wrote: All the aliases are fail to compile, have not found anything about it in any of the documentations I checked. Like the others said, alias just renames symbols, not expressions. Think of the generated code - if you are replacing a name, alias will work, but if you actually need to paste in some code that gets generated for each object, it won't. With the member variables or arrays, it changes a bit for each object. Alternatives here include: * just put the ubytes in your union. That's how I'd do it. (in fact, that is how I did it: https://github.com/adamdruppe/arsd/blob/master/color.d#L128 ) union { ubyte[4] components; /// [r, g, b, a] /// Holder for rgba individual components. struct { ubyte r; /// red ubyte g; /// green ubyte b; /// blue ubyte a; /// alpha. 255 == opaque } uint asUint; /// The components as a single 32 bit value (beware of endian issues!) } The anonymous struct wrapper inside the union is allowed and groups the 4 of them together as a single element inside the union. * Use a @property ref function to return the array element and trust the compiler to inline it.
Re: Alias on an array element
On Friday, 13 October 2017 at 02:04:03 UTC, Meta wrote: On Friday, 13 October 2017 at 01:12:38 UTC, solidstate1991 wrote: On Friday, 13 October 2017 at 01:09:56 UTC, solidstate1991 wrote: I'm making a struct for easy color handling Here's a code sample: ublic struct Color{ union{ uint raw; ///Raw representation in integer form, also forces the system to align in INT32. ubyte[4] colors; ///Normal representation, aliases are used for color naming. ubyte alpha, red, green, blue; } version(LittleEndian){ alias alpha = colors[0]; alias red = colors[1]; alias green = colors[2]; alias blue = colors[3]; }else{ alias alpha = colors[3]; alias red = colors[2]; alias green = colors[1]; alias blue = colors[0]; } } All the aliases are fail to compile, have not found anything about it in any of the documentations I checked. Edit: ubyte alpha, red, green, blue; was added so I can continue debugging after the refactoring until I find a solution. You can only create aliases for symbols, not expressions. You could create accessor functions that return the appropriate indices. Why not just do this: version(LittleEndian) { ubyte alpha, red, green, blue; } else { ubyte blue, green, red, alpha; } BTW. What platforms do you have in mind when thinking about BigEndian? I am curious because I usually consider BigEndian dead for my purposes.
Re: Alias on an array element
On Friday, 13 October 2017 at 01:12:38 UTC, solidstate1991 wrote: On Friday, 13 October 2017 at 01:09:56 UTC, solidstate1991 wrote: I'm making a struct for easy color handling Here's a code sample: ublic struct Color{ union{ uint raw; ///Raw representation in integer form, also forces the system to align in INT32. ubyte[4] colors; ///Normal representation, aliases are used for color naming. ubyte alpha, red, green, blue; } version(LittleEndian){ alias alpha = colors[0]; alias red = colors[1]; alias green = colors[2]; alias blue = colors[3]; }else{ alias alpha = colors[3]; alias red = colors[2]; alias green = colors[1]; alias blue = colors[0]; } } All the aliases are fail to compile, have not found anything about it in any of the documentations I checked. Edit: ubyte alpha, red, green, blue; was added so I can continue debugging after the refactoring until I find a solution. You can only create aliases for symbols, not expressions. You could create accessor functions that return the appropriate indices.
Re: Alias on an array element
On Friday, 13 October 2017 at 01:09:56 UTC, solidstate1991 wrote: I'm making a struct for easy color handling Here's a code sample: ublic struct Color{ union{ uint raw; ///Raw representation in integer form, also forces the system to align in INT32. ubyte[4] colors; ///Normal representation, aliases are used for color naming. ubyte alpha, red, green, blue; } version(LittleEndian){ alias alpha = colors[0]; alias red = colors[1]; alias green = colors[2]; alias blue = colors[3]; }else{ alias alpha = colors[3]; alias red = colors[2]; alias green = colors[1]; alias blue = colors[0]; } } All the aliases are fail to compile, have not found anything about it in any of the documentations I checked. Edit: ubyte alpha, red, green, blue; was added so I can continue debugging after the refactoring until I find a solution.
Alias on an array element
I'm making a struct for easy color handling Here's a code sample: ublic struct Color{ union{ uint raw; ///Raw representation in integer form, also forces the system to align in INT32. ubyte[4] colors; ///Normal representation, aliases are used for color naming. ubyte alpha, red, green, blue; } version(LittleEndian){ alias alpha = colors[0]; alias red = colors[1]; alias green = colors[2]; alias blue = colors[3]; }else{ alias alpha = colors[3]; alias red = colors[2]; alias green = colors[1]; alias blue = colors[0]; } } All the aliases are fail to compile, have not found anything about it in any of the documentations I checked.