On 07/22/2015 06:29 AM, Taylor Gronka wrote:
Hi,

I have a template function, and I want it to do something if the input
variable is a list of structs, and something else if the input is a struct.

1) What's the best way to test this? I suppose I can call
__traits(identifier, results) and look at the last two characters.
2) If `T` is a list of structs, how can I initialize a new struct so
that I can append to `T`?

This is sort of how it might be done in python. I haven't implemented
question 2 yet since I'm not sure how atm:

template Uks(T) {
     T query(string q) {
         T result;
         try {
             ulong test = result.length; // won't compile for non-list
structs
             // append to a struct list
             T.subtype newResult; // is there a function like this?
             result ~= newResult;
         } catch {
             // assign the struct members directly
         }
         return result;
     }
}

I would like to call it like either of the following, depending on if I
want 1 result or X results:
auto res = Uks!(U_Struct).query("email");
auto res = Uks!(U_Struct[]).query("email");

I'd be happy to hear better design methods.

Thanks

template Uks(T){
    T query(string q){
        T result;
        static if(is(T==S[],S)){
            ulong test=result.length;
            // append
            S newResult;
            result~=newResult;
        }else{
            // assign
        }
        return result;
    }
}

void main(){
    struct U_Struct{}
    auto res1=Uks!(U_Struct).query("email");
    auto res2=Uks!(U_Struct[]).query("email");
}

Reply via email to