various questions

Jason Spencer spencer8 at sbcglobal.net
Sat Jul 31 01:51:29 PDT 2010


== Quote from Rory Mcguire (rjmcguire at gm_no_ail.com)'s article
> Jason Spencer wrote:
> > == Quote from Rory Mcguire (rjmcguire at gm_no_ail.com)'s article
> >> Jason Spencer wrote:
> >
> >> > 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.

> >> 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.

This is the part I'm still not getting.  Why shouldn't

   foreach (t; 0..3)

work?  Those are integer constants.

Actually, I think I'm getting it.  str_types.length is actually (or
close to) an integer literal, but t is not.  t over a range is an int
variable.  So at best, the compiler will infer the type of t and try
to get TypeTuple![int] from the mixin, which doesn't help.  But it
works in Iota because it only needs a value, and the length property
is not a variable, but a compile-time constant.

I'm not sure what magic gets worked when t is bound to a TypeTuple
that has int literals, but I'm guessing t in that case is not an int
variable, but a compile-time type variable, and it iterates over int
literals.  Those work with templ.  What I really want to know is "does
that foreach run at compile-time or run-time?"  I suspect compile-time
because it iterates over type variables.  But documentation is shakey
:)



> I convert str_types.length to its actual value below:
> foreach (t; 3) {
> ...
> }
> You can't do that (dmd : t.d(6): Error: int is not an aggregate
type)

Yeah, I mis-typed orginally.  It was "foreach (t;
0..str_types.length)" as a range.


More information about the Digitalmars-d-learn mailing list