Tuple enhancement

2016-10-16 Thread Andrei Alexandrescu via Digitalmars-d
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

Re: Tuple enhancement

2016-10-16 Thread Dmitry Olshansky via Digitalmars-d
On 10/16/16 3:58 PM, 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); I swear I've seen a pull request merged that add

Re: Tuple enhancement

2016-10-16 Thread Andrei Alexandrescu via Digitalmars-d
On 10/16/2016 11:07 AM, Dmitry Olshansky wrote: On 10/16/16 3:58 PM, 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);

Re: Tuple enhancement

2016-10-16 Thread Sebastiaan Koppe via Digitalmars-d
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 tup

Re: Tuple enhancement

2016-10-16 Thread Chris Wright via Digitalmars-d
On Sun, 16 Oct 2016 18:51:06 +, 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

Re: Tuple enhancement

2016-10-16 Thread sarn via Digitalmars-d
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

Re: Tuple enhancement

2016-10-16 Thread Temtaime via Digitalmars-d
On Sunday, 16 October 2016 at 20:11:01 UTC, Chris Wright wrote: On Sun, 16 Oct 2016 18:51:06 +, 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.:

Re: Tuple enhancement

2016-10-16 Thread Kapps via Digitalmars-d
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

Re: Tuple enhancement

2016-10-17 Thread Sebastiaan Koppe via Digitalmars-d
On Sunday, 16 October 2016 at 20:11:01 UTC, Chris Wright wrote: On Sun, 16 Oct 2016 18:51:06 +, Sebastiaan Koppe wrote: That would mean that tuple then needs to keep the strings around - taking up space. It doesn't change Tuple.sizeof. Sure, but it still takes up space in the executable.

Re: Tuple enhancement

2016-10-17 Thread Jack Stouffer via Digitalmars-d
On Sunday, 16 October 2016 at 16:49:43 UTC, Andrei Alexandrescu wrote: Sadly it was closed, not merged. And it was a bit more general allowing getting a field by name from any struct. -- Andrei The PR in question if anyone is interested: https://github.com/dlang/phobos/pull/4154 I currently

Re: Tuple enhancement

2016-10-17 Thread Steven Schveighoffer via Digitalmars-d
On 10/16/16 9:58 AM, 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

Re: Tuple enhancement

2016-10-17 Thread Edwin van Leeuwen via Digitalmars-d
On Monday, 17 October 2016 at 13:35:12 UTC, Steven Schveighoffer wrote: Why not something like this: Val get(T, Val)(auto ref T item, string memberName, Val defaultValue) { switch(memberName) { foreach(n; __traits(allMembers, T)) { static if(is(typeof(__traits(getMember, ite

Re: Tuple enhancement

2016-10-17 Thread Steven Schveighoffer via Digitalmars-d
On 10/17/16 9:42 AM, Edwin van Leeuwen wrote: On Monday, 17 October 2016 at 13:35:12 UTC, Steven Schveighoffer wrote: Why not something like this: Val get(T, Val)(auto ref T item, string memberName, Val defaultValue) { switch(memberName) { foreach(n; __traits(allMembers, T)) {

Re: Tuple enhancement

2016-10-18 Thread Bienlein via Digitalmars-d
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"; In Scala you can do this: def getUserInfo = ("Al", 42, 20

Re: Tuple enhancement

2016-10-18 Thread Idan Arye via Digitalmars-d
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