std.mixins
Lutger
lutger.blijdestijn at gmail.com
Tue Aug 31 15:05:45 PDT 2010
Philippe Sigaud wrote:
...
>> I want this to work or something like it, is it possible?
>>
>> enum test = [1,2, 3];
>>
>> foreach(i; 0..test.length) {
>> pragma(msg, to!string(test[i]) );
>> }
>>
>
> Well, 0..someEnum is not recognized by the compiler. The workaround is to
> create a typetuple of the correct length and use it as a support for
> iteration:
>
> template ZeroTo(int to) if (to >= 0)
> {
> static if (to == 0)
> alias TypeTuple!() ZeroTo;
> else static if (to == 1)
> alias TypeTuple!(0) ZeroTo;
> else
> alias TypeTuple!(ZeroTo!(to-1), to-1) ZeroTo; // here I use the fact
> that integers are accepted directly as template parameters and that
> TypeTuple auto-flatten
> }
>
> so ZeroTo!3 is (0,1,2), since I made it a range open on the right. ZeroTo!0
> is empty...
>
> enum test = [1,2, 3];
>
> foreach(i; ZeroTo!(test.length)) {
> pragma(msg, to!string(test[i]) );
> }
>
> The from -> to version is easy, along with a step parameter.
>
>
> Philippe
wtf it works! That's a cool trick / workaround, thanks.
More information about the Digitalmars-d
mailing list