Consider I have the following classes:
/// File classes.d
class C {
string type() { return "C"; };
}
class C1 : C {
override string type() { return "C1"; };
}
class C2 : C {
override string type() { return "C1"; };
}
Now I want to implement a factory somewhere else, like:
/// File factory.d
module factory;
import std.functional;
import std.stdio;
void main() {
mixin(import("classes.d"));
auto c = cast(typeof(mixin("C1"))) Object.factory("C1");
writeln(c.type());
}
The compiler (dmd 2.058) says me:
factory.d(7): Error argument C1 to typeof is not an expression
I know the following line compiles well:
auto c = cast(C1) Object.factory("classes.C1");
but this requires me to know the type (C1) at compile time. I want to get the type at runtime (like a string).
I don’t understand you question, I think:
You want to cast dynamically at runtime to a given type? This is not possible in a statically typed language! What you can do is, passing your classes (or whatever) around as
void*and using a switch … case to cast it to the needed type (you can also usestd.variant, which is maybe the better way, but I’ve never used it) and then calling different functions (templated functions):You can also generate the switch case at compile-time:
If you just wanna get the string of the type
typeof(bar).stringofwill give you the type ofbaras string (but this is already known at compile time).