-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
For loop range is unusable with types other than usize #17039
Comments
I would also love to have more control over the index type. |
Oh well. is the problem just inference? Or is it not even possible to have the choice to manually choose what type the range is? I know that overdoing inference can be exponentially bad, I have written statements in Swift before that just couldn't compile :) The only workaround I can think of is emulating a C style loop with a Either way this feels a bit out of character for a language sold on the level of control it provides. |
Is there at least no performance cost when using |
In safe build modes ( Technically you could find some performance cost for originally allocating the |
@IntegratedQuantum hmm that comment was made before |
I did find one way to do something similar with comptime, it works for me: fn i32Range(comptime a: i32, comptime b: i32) [b - a]i32 {
comptime {
var range = std.mem.zeroes([b - a]i32);
for (range[0..], 0..) |*v, i| v.* = a + @as(i32, i);
return range;
}
}
// ...
// somewhere in code you do this
inline for (comptime i32Range(-1, 2)) |i| {
// I guess "i" is now comptime known and is of type "i32"
} Hope it helps anyone who stumbles in here searching similar issues like I did. |
Zig Version
0.11.0
Steps to Reproduce and Observed Behavior
I have this function:
where Sprite is
[8][8]Color
(Ignore that the function has bugs, that's not really the point 🙂)
Doesn't matter if I use
0..
or0..8
it still doesn't compile, as the range is seemingly inferred to be usize.The only way to make it work is to use the two casts as shown in the function above to make the compiler happy.
This is annoying because this range is compile time known, so it should be clear it can fit in a
u8
.Expected Behavior
I expected to be able to use
u8
in loops.The text was updated successfully, but these errors were encountered: