Template-style polymorphism in table structure

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 4 07:49:30 PDT 2016


On Sunday, 4 September 2016 at 14:24:12 UTC, data pulverizer 
wrote:
> On Sunday, 4 September 2016 at 14:20:24 UTC, data pulverizer 
> wrote:
>> On Sunday, 4 September 2016 at 14:07:54 UTC, data pulverizer 
>> wrote:
>> @Lodovico Giaretta Thanks I just saw your update!
>
> @Lodovico Giaretta BTW what do you mean that my code is not 
> very D style? Please expand on this ...

The constructors can be less. In fact, a typesafe variadic ctor 
also works for the single element case and for the array case. 
But you already recognized that.

Instead of reinventing the wheel for your GenericVector!T, you 
could use an `alias this` to directly inherit all operation on 
the underlying array, without having to reimplement them (like 
your append method).

Your getCol(i) could become getCol!T(i) and return an instance of 
GenericVector!T directly, after checking that the required column 
has in fact that type:

GenericVector!T getCol!T(size_t i)
{
     if(typeid(cols[i]) == typeid(GenericVector!T))
         return cast(GenericVector!T)cols[i];
     else
         // assert(0) or throw exception
}

Another solution: if you don't need to dynamically change the 
type of the columns you can have the addColumn function create a 
new type. I show you with Tuples because it's easier:

Tuple!(T,U) append(U, T...)(Tuple!T tup, U col)
{
     return Tuple!(T,U)(tup.expand, col);
}

Tuple!int t1;
Tuple!(int, float) t2 = t1.append(2.0);
Tuple!(int, float, char) t3 = t2.append('c');


More information about the Digitalmars-d-learn mailing list