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);

The get method takes the field name and the type of the presumed field, and it returns the value of the field in the tuple. If no field by that name and type, return the second argument.

Rquirements:

* Do not throw - allow the second argument to be a throwing delegate

* Do not add overhead if the method is never used

* Figure out a reasonable (but not all too complicated) way to deal with implicit conversions, e.g. if x == "a" in the the code above, should it return 3.14 or convert the int to double?

* Handle qualifiers appropriately


Andrei

Personally, if I wanted this, I'd be using a hash table with Variant objects in it or something. An argument for keeping tuple features simple is that they really aren't a good tool for doing anything fancy. If you need a runtime interface, you're probably outgrowing tuples already. Why not make a struct that contains a hash table?

Reply via email to