On Sunday, 16 October 2016 at 18:51:06 UTC, Sebastiaan Koppe wrote:
On Sunday, 16 October 2016 at 13:58:51 UTC, Andrei Alexandrescu wrote:
I was thinking it would be handy if tuples had a way to access a field by name at runtime. E.g.:

Tuple!(int, "a", double, "b") t;
string x = condition ? "a" : "b";
double v = t.get!string(x, 3.14);

That would mean that tuple then needs to keep the strings around - taking up space. That plus the fact that the following code is equivalent (obviously):

Tuple!(int, "a", double, "b") t;
double v = condition ? t.a : t.b;

Why this addition? What does it enable?

I don't think this would actually require storing any additional data. It'd be more like:

struct Tuple(Aliases...) {
    auto get(T)(string name) {
        foreach(alias, i; ExtractNames!Aliases) {
            if(alias == name)
                return this[i];

Which is the advantage over runtime reflection in this case. It does seem a bit arbitrary to have Tuple include it though. But given that it's a template method (therefore won't be compiled in if not accessed for this particular Tuple type) and requires storing no data in the instance, there's zero overhead if you're not actually using the feature.

Reply via email to