digitalmars.D.learn - How to mixin each element of tuple
- Michal Minich (13/13) Dec 20 2011 My naive approach doesn't works
- Timon Gehr (38/51) Dec 20 2011 This will do what you want:
- Michal Minich (5/21) Dec 21 2011 Thank you. Solution 1 worked well.
- bearophile (4/8) Dec 20 2011 In D if you use foreach on a typeTuple you get a static foreach.
- Timon Gehr (5/13) Dec 20 2011 Yes, but foreach cannot be used in declaration scope. I think having
- bearophile (5/6) Dec 20 2011 Vote here! :-)
My naive approach doesn't works struct Item1 (T) {} struct Item2 (T) {} struct Group (Items ...) { // how to do this? ... no static foreach :( static foreach (I; Items) mixin I!(int); } void main () { alias Group!(Item1, Item2) G; }
Dec 20 2011
On 12/20/2011 02:32 PM, Michal Minich wrote:My naive approach doesn't works struct Item1 (T) {} struct Item2 (T) {} struct Group (Items ...) { // how to do this? ... no static foreach :( static foreach (I; Items) mixin I!(int); } void main () { alias Group!(Item1, Item2) G; }This will do what you want: Solution 1: struct Item1(T){} struct Item2(T){} mixin template getItems(Items ...){ static if(Items.length) { private alias Items[0] _item; mixin _item!int; mixin getItems!(Items[1..$]); } } struct Group(Items...) { mixin getItems!Items; } void main(){ alias Group!(Item1, Item2) G; } Solution 2: struct Item1(T){} struct Item2(T){} struct Group(Items...) { private static string gen(){ string r; foreach(i;0..Items.length){ import std.conv; r~=`private alias Items[`~text(i)~`] _item`~text(i)~`;`; r~=`mixin _item`~text(i)~`!int;`; } return r; } mixin(gen()); } void main(){ alias Group!(Item1, Item2) G; } But I'd rather have static foreach too.
Dec 20 2011
On 20. 12. 2011 16:20, Timon Gehr wrote:struct Item1(T){} struct Item2(T){} mixin template getItems(Items ...){ static if(Items.length) { private alias Items[0] _item; mixin _item!int; mixin getItems!(Items[1..$]); } } struct Group(Items...) { mixin getItems!Items; } void main(){ alias Group!(Item1, Item2) G; }Thank you. Solution 1 worked well. The need to introduce new symbol '_item' using alias because one cannot mixin Items[0]!int directly seems to me as a bug. Do you happen to know if it is already reported?
Dec 21 2011
Michal Minich:struct Group (Items ...) { // how to do this? ... no static foreach :( static foreach (I; Items)In D if you use foreach on a typeTuple you get a static foreach. Bye, bearophile
Dec 20 2011
On 12/20/2011 07:13 PM, bearophile wrote:Michal Minich:Yes, but foreach cannot be used in declaration scope. I think having static foreach would greatly benefit the language. I regularly run into cases where I have to use one of the two inferior solutions when static foreach would be just the right tool for the job.struct Group (Items ...) { // how to do this? ... no static foreach :( static foreach (I; Items)In D if you use foreach on a typeTuple you get a static foreach. Bye, bearophile
Dec 20 2011
Timon Gehr:I think having static foreach would greatly benefit the language.Vote here! :-) http://d.puremagic.com/issues/show_bug.cgi?id=4085 Bye, bearophile
Dec 20 2011