www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Casting between tuples

reply Sean Eskapp <eatingstaples gmail.com> writes:
I have a variable of type TypeTuple!(int, int), and I want to convert all its
elements into a variable of type TypeTuple!(string, string). Is there a way to
do this? Using a loop fails compilation with "Error: Integer constant
expression expected instead of i".

I'd also like to be able to convert from arrays of base classes (static size)
to TypeTuples of derived classes. "static for" doesn't work either. Is there a
way to do this.. without a recursive template function?
Jan 12 2011
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Sean Eskapp <eatingstaples gmail.com> wrote:

 I have a variable of type TypeTuple!(int, int), and I want to convert  
 all its
 elements into a variable of type TypeTuple!(string, string). Is there a  
 way to
 do this? Using a loop fails compilation with "Error: Integer constant
 expression expected instead of i".

Just to be sure, you want something( TypeTuple!(int, int) ) to yield TypeTuple!( TypeTuple!(string, string), TypeTuple!(string, string) )? This could be done by means of std.traits' staticMap in conjunction with a custom template: template replace( T ) { alias TypeTuple!(string, string) replace; } alias staticMap!( replace, TypeTuple!( int, int ) ) myNewTuple; static assert( myNewTuple.stringof == TypeTuple!( TypeTuple!(string, string), TypeTuple!(string, string) ).stringof );
 I'd also like to be able to convert from arrays of base classes (static  
 size)
 to TypeTuples of derived classes. "static for" doesn't work either. Is  
 there a
 way to do this.. without a recursive template function?

Not that I know of. Recursive templates are fun, though. -- Simen
Jan 12 2011
prev sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Sean Eskapp Wrote:

 I have a variable of type TypeTuple!(int, int), and I want to convert all its
 elements into a variable of type TypeTuple!(string, string). Is there a way to
 do this? Using a loop fails compilation with "Error: Integer constant
 expression expected instead of i".
 
 I'd also like to be able to convert from arrays of base classes (static size)
 to TypeTuples of derived classes. "static for" doesn't work either. Is there a
 way to do this.. without a recursive template function?

Maybe I don't understand what you want, but it sounds like you want to convert runtime information into compile time information? If you have a Tuple of int you can't convert the elements to string because you don't know what they are at compile time. Converting a TypeTuple doesn't make sense all as it has is Type information and if you want a TypeTuple!(string, string) then use that. auto something = cast(string) int; what? An array of base class, Base[3] foo does not make sense to convert to TypeTuple!(Dirived1, Dirived2, Dirived1) bar. But if you know that is how Base class is laid out then you can convert them to the proper type (I don't think this will compile, and if it did, wouldn't do what you expected): foreach(i, item; foo) auto myClass = to!(bar[i])(item); But that is still a weird. what is you are trying to do?
Jan 12 2011