On 08/31/2015 08:55 PM, WhatMeWorry wrote:
> Thanks for all the above suggestions, but after many hour of re-reading
> Ali's book on template, structs, and mixins, I still in the woods. I've
> tried two approaches:
>
> ================ Templatetized struct ================================
>
> struct Chameleon(T, Purpose p)
> {
> static if (p == Purpose.POSITIONAL)
> {
> struct Positional { T x, y, z; }
> }
> else static if (p == Purpose.COLOR)
> {
> struct Color_No_Alpha { T r, g, b; }
> }
> };
(Aside: You don't need that semicolon in D.)
So, you have a struct template that contains another struct type, either
a Positional or a Color_No_Alpha.
Note that the outer struct has no members at all: It merely defines two
struct types. I can do the following but I don't think it's what you want:
auto x = Chameleon!(float, Purpose.POSITIONAL).Positional(1.5, 2.5, 3.5);
> struct VertexData
> {
> Chameleon!(float, purpose.POSITIONAL) position;
> Chameleon!(float, purpose.COLOR) color;
> }
You have a couple of typos there: purpose should be capitalized.
However, I see that you had the following in your original code:
enum Purpose { POSITIONAL, COLOR_ONLY, COLOR_AND_ALPHA, GENERIC_TRIPLE,
GENERIC_QUAD }
Purpose purpose;
// ...
Chameleon!(float, purpose.POSITIONAL) position;
I am surprised that it works! Must be a "feature". ;) The common thing
to do is to remove the 'purpose' variable and use the enum type directly:
enum Purpose { /* ... */ }
// ...
Chameleon!(float, Purpose.POSITIONAL) position;
> alias Vert = VertexData;
Fine.
> alias Pos = VertexData.position;
> alias RGB = VertexData.color;
Those two don't make sense because both .position and .color are member
variables. If you did the following, then we would be talking about the
types of those members:
alias Pos = typeof(VertexData.position);
alias RGB = typeof(VertexData.color);
> VertexData[] vertices =
> [
> VertexData( Pos(1.0, 1.0, 1.0), RGB(0.0, 0.0, 0.0)) // Compiler
error
> ];
However, that won't work because Chameleon does not have any members.
The same with the mixin template...
I am copying my earlier code with a couple of aliases to make closer to
yours:
enum Purpose { POSITIONAL, COLOR_ONLY }
struct Chameleon(T, Purpose p)
{
static if (p == Purpose.POSITIONAL) {
T x, y, z;
} else static if (p == Purpose.COLOR_ONLY) {
T r, g, b;
} else {
static assert(false, "Not yet supported");
}
}
alias Pos = Chameleon!(float, Purpose.POSITIONAL);
alias Color = Chameleon!(float, Purpose.COLOR_ONLY);
struct VertexData
{
Pos position;
Color color;
}
alias Vert = VertexData;
VertexData[] vertices =
[
Vert(Pos(1.0f, 1.0f, 1.0f), Color(0.0, 0.0, 0.0))
];
void main()
{}
Ali