How to mixin each element of tuple
Timon Gehr
timon.gehr at gmx.ch
Tue Dec 20 07:20:15 PST 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.
More information about the Digitalmars-d-learn
mailing list