Tuple enhancement
Edwin van Leeuwen via Digitalmars-d
digitalmars-d at puremagic.com
Mon Oct 17 06:42:18 PDT 2016
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.
More information about the Digitalmars-d
mailing list