Search code examples
templatesclassstructd

Declare class member at runtime in D


I want the following struct as a class member, but I don't know the type of T, so I need "declare" the struct at runtime.


struct Chunk (T) {
    string id;
    T[][] data;
}

class FileBla {
    this() {
        Chunk !int ck; // need to be turned in a class member
    }
}

Should be missing something easy.


Solution

  • You can template the class as well:

    import std.stdio;
    
    struct Chunk (T) {
        string id;
        T[][] data;
    }
    
    class FileBla(T) {
    private:
        Chunk!T ck;
    }
    
    void main() {
        auto f = new FileBla!int;
        writeln(typeid(f.ck));
    }