Tuple enhancement

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 17 06:54:56 PDT 2016


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))
>>    {
>>       static if(is(typeof(__traits(getMember, item, n)) : Val))
>>         case n: mixin("return item." ~ n ~ ";");
>>    }
>>    default:
>>      return defaultValue;
>>    }
>> }
>
> I like the general solution, although I probably would just expect it to
> fail compiling if no member is present or return Val.init if no
> defaultValue is passed.

Hm... this could work:

Val get(Val, T)(auto ref T item, string memberName, Val defaultValue = 
Val.init)

int x = t.get("a", 1);
int x = t.get!int("a");

-Steve


More information about the Digitalmars-d mailing list