digitalmars.D.learn - Casting between tuples
- Sean Eskapp (7/7) Jan 12 2011 I have a variable of type TypeTuple!(int, int), and I want to convert al...
- Simen kjaeraas (14/25) Jan 12 2011 Just to be sure, you want something( TypeTuple!(int, int) ) to yield
- Jesse Phillips (11/19) Jan 12 2011 Maybe I don't understand what you want, but it sounds like you want to c...
- Jesse Phillips (3/3) Jan 12 2011 Wow, missed the part where TypeTuples can hold values. I suggest looking...
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
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
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
Wow, missed the part where TypeTuples can hold values. I suggest looking into using Tuple from std.typecons. What you need to remember is that you are not casting the tuple, you are casting the data in the tuple which creates a completely different tuple. It seems you are trying to use the type system to be sure of the class type at compile time so that you don't have null values when you cast to the derived type. If this is the case then note that it can't be done.
Jan 12 2011