Passing struct and struct[] into a template
Taylor Gronka via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jul 21 21:29:23 PDT 2015
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
More information about the Digitalmars-d-learn
mailing list