Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
December 20, 2011 How to mixin each element of tuple | ||||
---|---|---|---|---|
| ||||
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; } |
December 20, 2011 Re: How to mixin each element of tuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michal Minich | 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.
|
December 20, 2011 Re: How to mixin each element of tuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michal Minich | 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
|
December 20, 2011 Re: How to mixin each element of tuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
December 20, 2011 Re: How to mixin each element of tuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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 |
December 21, 2011 Re: How to mixin each element of tuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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?
|
Copyright © 1999-2021 by the D Language Foundation