-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Infer type on ranged for loops from the type of the range values instead of defaulting to usize #14704
Comments
I think we definitely need some way of having control over the index type. While looking for |
Just to add my "+1", I just stumbled onto this issue in actual code, here was my workaround. for (0..3) |lod| {
const lod_u4 = @intCast(u4, lod);
world_patch_manager.WorldPatchManager.getLookupsFromRectangle(state.heightmap_patch_type_id, area_old, lod_u4, &lookups_old); The lod is a u4 because it doesn't need to be more. Since the for loop pretty clearly/explicitly fits inside a u4, I think it should work fine to just pass it in. Just like it does when you explicitly pass in, say, 3, you don't need to do Code here (at the time of writing only |
Any chance we can get some elaboration on why this has been closed? |
I'm not accepting language proposals at this time. I've already considered this and rejected it. If you tried to implement your proposal, you would see the problems with it. Unfortunately I don't have time to explain in detail why nearly all of the ~463 open proposals will be rejected. |
Ranged loops defaulting to usize only makes sense when it's paired with a slice or array iteration, in any other case there isn't really any good reason for it that I can think of, especially when the values used aren't
comptime_int
s.The following is the behavior I would expect from the types on ranged for loops:
In discussion on this topic in the discord, the idea of putting the type on the capture was also floated:
But I think basing it off the type of the values used in the range itself makes much more sense, personally.
There was also a suggestion to infer the integer type to the smallest type that will fit the range when using
comptime_int
to define the range.But requiring an explicit type in there somewhere feels more "correct" to me, and I feel that the uses where you know both ends of the range at comptime and aren't iterating over an array or storing the number in a constant somewhere that can be give a type are pretty uncommon in practice.
The text was updated successfully, but these errors were encountered: