xxxxxxxxxx
struct Pack(Ts...)
{
private Ts elements;
// Access see my DIP; no alias this! Uses opStaticIndex.
Ts[i] opStaticIndex(size_t i)() { return elements[i]; }
enum length = elements.length;
enum opDollar = elements.length;
auto opDispatch(string memberName)()
{
return mixin((){
string result = "pack[";
static foreach (i; 0 .. elements.length)
{
result ~= mixin(`"elements[`,i,`].`, memberName, `,"`);
}
return result ~ "]";
}());
}
}
// makes pack[ x1, x2, x3 ] work and return a properly typed Pack.
struct pack
{
static Pack!Ts opIndex(Ts...)(Ts args)
{
return Pack!Ts(args);
}
}
struct S1
{
int member;
long other;
}
struct S2
{
double member;
string other;
}
unittest
{
auto p = pack[ S1(1, long.max), S2(2.0, "twelve") ];
auto q = p.member;
static assert(is(typeof(q) == Pack!(int, double)));
static assert(is(typeof(q.opStaticIndex!0) == int));
static assert(is(typeof(q.opStaticIndex!1) == double));
assert(q.opStaticIndex!0 == 1);
assert(q.opStaticIndex!1 == 2.0);
}