3 ms·
This is arguably a limitation of C-style for loops. For example, Python: for i in range(0, 256): Rust: for i in 0..256 { and many other languages hav
by sunfish 8y ago
This is arguably a limitation of C-style for loops.
For example, Python:
for i in range(0, 256):
Rust:
for i in 0..256 {
and many other languages have similar things.
Proper ranges also mean that the compiler never needs to do complicated reasoning that may depend on signed integer overflow having undefined behavior in order to prove that the loop has a finite number of iterations or that `i` doesn't wrap around.
- Sharlin 8y agoYes, I considered mentioning Rust as a more modern example. But even in Rust, ranges such as `0..max+1` are awkward enough that a closed range syntax was added to the language. With a range like `0u8..=255u8` you can also ensure that your induction variable can directly be passed to things expecting a u8 without a cast that might hide an accidental overflow.