Static convertability testing?
Chris Williams via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Feb 12 11:09:45 PST 2015
I have a template function that gets values out of a tree of
variant types. My goal is to be able to write code like;
node.get!string("path", "to", "leaf");
Inside get(), I would like to use std.conv to dynamically convert
(where able) to the target type (T) or, if that is not possible,
to return T.init.
If I wanted to force the user to request the correct type as is
stored in the structure, I could write code like:
switch (leafType) {
case STRING:
static if (is(T : string)) {
return leftValue;
}
else {
return T.init
}
break;
...
But since I want to allow all possiblities that std.conv
supports, I want something like:
static if (isConvertible!(T, string)) {
return leftValue.to!T;
}
else {
return T.init;
}
Is there something like isConvertible() in the library somewhere?
More information about the Digitalmars-d-learn
mailing list