I am trying to build a data table object with unrestricted column types. The approach I am taking is to build a generic interface BaseVector class and then a subtype GenericVector(T) which inherits from the BaseVector. I then to build a Table class which contains columns that is a BaseVector array to represent the columns in the table.

My main question is how to return GenericVector!(T) from the getCol() method in the Table class instead of BaseVector.

Perhaps my Table implementation somehow needs to be linked to GenericVector(T) or maybe I have written BaseTable instead and I need to do something like a GenericTable(T...). However, my previous approach created a tuple type data object but once created, the type structure (column type configuration) could not be changed so no addition/removal of columns.


------------------------------------------------
import std.stdio : writeln, write, writefln;
import std.format : format;

interface BaseVector{
    BaseVector get(size_t);
}

class GenericVector(T) : BaseVector{
    T[] data;
    alias data this;
    GenericVector get(size_t i){
        return new GenericVector!(T)(data[i]);
    }
    this(T[] arr){
        this.data = arr;
    }
    this(T elem){
        this.data ~= elem;
    }
    void append(T[] arr){
        this.data ~= arr;
    }

    override string toString() const {
        return format("%s", data);
    }
}

class Table{
private:
    BaseVector[] data;
public:
    // How to return GenericVector!(T) here instead of BaseVector
    BaseVector getCol(size_t i){
        return data[i];
    }
    this(BaseVector[] x ...){
        foreach(col; x)
            this.data ~= col;
    }
    this(BaseVector[] x){
        this.data ~= x;
    }
    this(Table x, BaseVector[] y ...){
        this.data = x.data;
        foreach(col; y){
            this.data ~= col;
        }
    }
    void append(BaseVector[] x ...){
        foreach(col; x)
            this.data ~= x;
    }
}


void main(){
    auto index = new GenericVector!(int)([1, 2, 3, 4, 5]);
auto numbers = new GenericVector!(double)([1.1, 2.2, 3.3, 4.4, 5.5]); auto names = new GenericVector!(string)(["one", "two", "three", "four", "five"]);
    Table df = new Table(index, numbers, names);
    // I'd like this to be GenericVector!(T)
    writeln(typeid(df.getCol(0)));
}

Reply via email to