digitalmars.D.learn - Strange template instantiating behaviour
- ref2401 (58/58) Jan 12 2013 public template rgba(uint value)
- Era Scarecrow (8/41) Jan 12 2013 Your ctor still is accepting arguments in rgba order (vs argb)
public template rgba(uint value) { enum Color rgba = Color( cast(ubyte)((value >> 24) & 0xFF), cast(ubyte)((value >> 16) & 0xFF), cast(ubyte)((value >> 8) & 0xFF), cast(ubyte)(value & 0xFF)); } public struct Color { public static immutable Color black = rgba!0xFF; public ubyte r = 0; public ubyte g = 0; public ubyte b = 0; public ubyte a = 0; public this(ubyte r, ubyte g, ubyte b, ubyte a) { this.r = r; this.g = g; this.b = b; this.a = a; } public string toString() const { return std.string.format("(r: %s, g: %s, b: %s, a: %s)", r, g, b, a); } } void main() { Color templateValue = rgba!0x00_00_00_FF; Color ctorValue = Color(0, 0, 0, 255); writefln("[template result] %s", templateValue); writefln("[ctor. result] %s", ctorValue); readln(); } Console output: [template result] (r: 0, g: 0, b: 0, a: 255) [ctor. result] (r: 0, g: 0, b: 0, a: 255) Now, if we change fields order in the struct we will get strange template instantiating behaviour. public struct Color { public static immutable Color black = rgba!0xFF; public ubyte a = 0; // "a" is the last field in the previous example public ubyte r = 0; public ubyte g = 0; public ubyte b = 0; ... } ... Console output: [template result] (r: 0, g: 0, b: 255, a: 0) [ctor. result] (r: 0, g: 0, b: 0, a: 255) if we change templateValue variable or Color.black value(or even comment it) all works correct. is it a bug?
Jan 12 2013
On Saturday, 12 January 2013 at 19:15:37 UTC, ref2401 wrote:public template rgba(uint value) { enum Color rgba = Color( cast(ubyte)((value >> 24) & 0xFF), cast(ubyte)((value >> 16) & 0xFF), cast(ubyte)((value >> 8) & 0xFF), cast(ubyte)(value & 0xFF)); } public struct Color { public static immutable Color black = rgba!0xFF; public ubyte r = 0; public ubyte g = 0; public ubyte b = 0; public ubyte a = 0; public this(ubyte r, ubyte g, ubyte b, ubyte a) { this.r = r; this.g = g; this.b = b; this.a = a; }Now, if we change fields order in the struct we will get strange template instantiating behaviour. public struct Color { public static immutable Color black = rgba!0xFF; // "a" is the last field in the previous example public ubyte a = 0; public ubyte r = 0; public ubyte g = 0; public ubyte b = 0;<snip>Console output: [template result] (r: 0, g: 0, b: 255, a: 0) [ctor. result] (r: 0, g: 0, b: 0, a: 255) if we change templateValue variable or Color.black value(or even comment it) all works correct. is it a bug?Your ctor still is accepting arguments in rgba order (vs argb) while your template says clearly the alpha channel is the lowest 8 bytes (the a in the first instance, or b in the last); Thereby from the looks of it, it is working correctly. If you remove the constructor entirely, then both of them should act the same, as then it's based on byte order (but a/alpha won't be the last one).
Jan 12 2013