in a template argument, specify which object member to access?

cy via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 8 13:09:47 PST 2016


object.member lets me access the member of the object, but what 
if I want to access those members in a generic way, but in a 
different arrangement depending on context? Like if I wanted to 
first follow a tree down, and second priority would be going left 
to right, but then I wanted to first go right, and second 
priority would be going down to up.

struct A {
	string up;
	string down;
	string left;
	string right;
}

template goPlaces(D1,D2,D3) {
	string go(A a) {
		return "go " ~ a.D1 ~ " then go " ~ a.D2 ~ " then go " ~ a.D3;
	}
}

import std.stdio;

void main() {
	A a = {"north","south","east","west"};
	writeln(goPlaces!(up,down,left)(a));
}

Do I just have to use a mixin? Or implement those members as 
indexes in an array, if I want to use them differently?

enum Way { UP, DOWN, LEFT, RIGHT };
struct A {
	string[Way.max+1] ways;
}
...


More information about the Digitalmars-d-learn mailing list