On 11/09/2016 07:44 AM, Is it possible to store different generic types?
wrote:
Is it possible to store different generic types in ex. somekind of
container such as an array, hashtable etc.
Let's say we got
class Foo(T) {
...
}
Would it be possible to store something like
Foo[] foos; // Where Foo of course should allow any generic version of Foo
Ex.
Foo!int and Foo!string should both be able to be stored inside the array.
If so how would one construct an array like that or is there some other
container that may be able to do it?
The classic way of doing it is inheriting from an interface. Usually
there is no casting needed:
import std.stdio;
import std.algorithm;
interface Foo {
void foo();
}
class SimpleFoo(T) : Foo {
void foo() {
writefln("foo'ing for %s", T.stringof);
}
}
void main() {
Foo[] foos;
foos ~= new SimpleFoo!int();
foos ~= new SimpleFoo!double();
foos.each!(f => f.foo());
}
Ali