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.

Reply via email to