For-loop counter other than usize

This much I agree with, to quote myself on that very subject:

I don’t think being able to write code such as

for (-7..7) |i: i16| {
    // ...
}

Would spoil the Zen of the language. It completes the process which started with adding ranged for loops.

It adds one more pinch of syntax, one which has only one possible interpretation, and in return, we get something which is fully general. I don’t care for the whiff of the arbitrary in language features, I figure that since we have ranged for loops, they should be able to be ranged over whatever values, using whatever type, the user wants.

To reiterate a point (not quoting myself twice!) it’s perfectly reasonable for the core team to not want to direct effort toward this. I would have preferred to see the issue stay open, in the event that someone decided to tackle it on their own, but having open issues itself imposes a certain maintenance burden, so I understand that choice as well.

If and when ranged integer types get added, that opens the door to a very nice feature from Pascal and Ada, namely arrays of a ranged type. Those can start and stop at any offset, so you could have something like [-7..7]u64 or what have you. Since the compiler would enforce that they only be indexed with the associated range type, there’s no need for bounds checking either. Coercion to a slice is… a bit of a wrinkle, admittedly, although just slicing it doesn’t have the same problem.

That would be great, and it basically does require the full for range specification to get full use out of it. So there may be an opportunity to revisit the question down the road.

1 Like