Constructing a tuple dynamically

Pena via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 3 02:32:45 PST 2015


I want to create a simple clone operator based on UDAs. First
step is to create a tuple of the fields with desired UDA value
and construct a clone method which uses that tuple.

import std.traits;

enum Cloneable;

struct Foo {
    @Cloneable int cloneableInt;
    @Cloneable string cloneableStr;
    float notCloneable;
}


I came up with this to iterate through fields and print out ones
with @Cloneable set:

foreach(member; __traits(allMembers, Foo)) {
    foreach(attr; __traits(getAttributes, mixin(member))) {
      static if(is(attr == Cloneable)) {
        pragma(msg, member);
      }
    }
}

How can I use this code or something similar to dynamically
construct a tuple containing types of fields marked as @Cloneable?


More information about the Digitalmars-d-learn mailing list