www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to mixin each element of tuple

reply Michal Minich <michal.minich gmail.com> writes:
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
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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
parent Michal Minich <michal.minich gmail.com> writes:
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
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 12/20/2011 07:13 PM, bearophile wrote:
 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
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.
Dec 20 2011
parent bearophile <bearophileHUGS lycos.com> writes:
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