Alias on an array element

Adam D. Ruppe destructionator at gmail.com
Fri Oct 13 13:22:42 UTC 2017


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.


More information about the Digitalmars-d-learn mailing list