3 ms·
> I guess what you want can be done at compile time Yes. > but probably it's a lot more work No, at least not if you want to limit yourself to arrays, this i
by tom_mellior 6y ago
> I guess what you want can be done at compile time
Yes.
> but probably it's a lot more work
No, at least not if you want to limit yourself to arrays, this is a trivial thing for a compiler frontend to handle. For example, the parser could already translate the "iterator for" loop syntax into exactly the same representation as it would produce for the "counter for" loop syntax.
The javac compiler for java does this: It transforms loops over arrays using iterator syntax into "normal" counter loops. Loops using iterator syntax over non-arrays are compiled to use iterator objects.
- steveklabnik 6y agoAs an example of this: https://doc.rust-lang.org/stable/std/iter/index.html#for-loops-and-intoiterator https://doc.rust-lang.org/stable/std/iter/index.html#for-loo...
- tom_mellior 6y agoI don't see iterator syntax being desugared to a counted loop there. I don't doubt that the Rust compiler eventually produces a counted loop when iterating over a vector, but this link doesn't demonstrate that.
- steveklabnik 6y agoIt’s not a counted loop, it’s true. for loops are sugar for iterators and an infinite loop + break. My point is that it is not a particularly complex transformation, as you said. I thought the complexity (or lack thereof) was the high order bit. Sorry I should have been more clear!