compile-time opIndex
Artur Skawina via Digitalmars-d
digitalmars-d at puremagic.com
Wed Dec 31 13:34:07 PST 2014
On 12/18/14 16:43, Steven Schveighoffer via Digitalmars-d wrote:
> We currently have the ability to do opIndex for simulating an array or other type indexed at runtime.
>
> But we have no way to simulate the ability of tuple indexing. Such an ability would unleash a huge amount of possibilities, including user-defined tuple types.
struct TupleWannabe(S) {
typeof(S.tupleof) tupleof;
static __memberWannabe() {
string s;
foreach (I, _; typeof(S.tupleof))
s ~= `ref `~S.tupleof[I].stringof~`() @property {
return tupleof[`~I.stringof~`];
} `;
return s;
}
mixin(__memberWannabe());
alias tupleof this;
}
struct S {
int key;
string val;
}
alias ST = TupleWannabe!S;
void main() {
ST s = ST(1, "blah");
s.key = 2;
s.val = "yada";
s[0] = 42;
s[1] = "etc";
}
SCNR. Happy new year!
artur
More information about the Digitalmars-d
mailing list