various questions

Rory Mcguire rjmcguire at gm_no_ail.com
Thu Jul 29 23:53:28 PDT 2010


Jason Spencer wrote:

> Ok, I've gone over this, adapted it, and mostly understand it.  I just
> have one question left:
> 
> == Quote from bearophile (bearophileHUGS at lycos.com)'s article
>> template Iota(int stop) {
>> ...
>>         alias TypeTuple!(Iota!(stop-1), stop-1) Iota;
>> }
>> ...
>>     foreach (t; Iota!(str_types.length))
> 
> What happens at compile-time with this foreach loop?
> 
> I nievely went and replaced "foreach (t; Iota!(str_types.length))"
> with "foreach (t; str_types.length)", since the length of that array
> is known at compile-time.  That of course bombed, but I don't quite
> get why.  Is the compiler actually evaluating the foreach loop at
> compile time?  How could it, when the body makes run-time checks?  If
> it's not, why doesn't my change work?
> 
> Jason

your replacement tries to loop over an uint called str_types.length. Never 
gonna happen.

Iota!(str_types.length) seems to generate str_types.length(a number of) 
integer indexes. Can't use 0 .. str_types.length in the foreach because 
compiler is expecting Integer constants so it can make the template "foo" 
into actual code.




More information about the Digitalmars-d-learn mailing list