Alias on an array element

Igor stojkovic.igor at gmail.com
Fri Oct 13 09:02:46 UTC 2017


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.



More information about the Digitalmars-d-learn mailing list