turning an array of structs into a struct of arrays

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 3 14:22:07 PDT 2015


On Friday, 3 July 2015 at 10:52:03 UTC, Laeeth Isharc wrote:
> I have an array of structs eg
>
> struct PriceBar
> {
>   DateTime date;
>   double open;
>   double high;
>   double low;
>   double close;
> }
>
> (which fields are present in this particular struct will depend 
> on template arguments).
>
> what is the best way to turn these at compile time into a 
> struct of arrays? eg
>
> struct PriceBars
> {
>   DateTime[] dates;
>   double[] opens;
>   double[] highs;
>   double[] lows;
>   double[] closes;
> }
>
> I can use FieldTypeTuple and FieldNameTuple, but I am a bit 
> lost as to how without static foreach to loop through these in 
> order to generate a mixin to declare the new type.  I can turn 
> it into a string, but what is the better option?
>
> Thanks.

Not necessarily the best solution to the problem, but I just 
wanted to show this because I think it's kinda cool:

https://gist.github.com/John-Colvin/103d3c064ad6cb4cf435#file-transformmembers-d


More information about the Digitalmars-d-learn mailing list