Template-style polymorphism in table structure
data pulverizer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Sep 5 01:11:14 PDT 2016
On Monday, 5 September 2016 at 06:45:07 UTC, data pulverizer
wrote:
> On Sunday, 4 September 2016 at 14:49:30 UTC, Lodovico Giaretta
> wrote:
>> 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
>> }
> I just realized that typeid only gives the class and not the
> actual type, so the object will still need to be cast as you
> mentioned above, however your above function will not infer T,
> so the user will have to provide it. I wonder if there is a way
> to dispatch the right type by a dynamic cast or I fear that
> ZombineDev may be correct and the types will have to be
> limited, which I definitely want to avoid!
Just found this on dynamic dispatching
(https://wiki.dlang.org/Dispatching_an_object_based_on_its_dynamic_type) but even if you took this approach, you'd still have to register all the types you would be using at the start of the script for all your methods. It's either that or explicitly limited type list as ZombineDev suggests.
More information about the Digitalmars-d-learn
mailing list