-
-
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
'comptime_int' error when switching on caught Error #7179
Comments
This section on ZigLearn explains that literal values are of type
Oh well, I didn't see the |
This also seems like a bug because it should be showing the error that the |
I think I have the same kind of error... I am trying to compile this: fn center(text: u8, out: *[5]u8) !void {
const left_pad: usize = if (text < 10) 3 else if (text < 100) 2 else 1;
... Looks straightforward but doesn't compile because the if expression tries to take the type I also tried this: fn center(text: u8, out: *[5]u8) !void {
const left_pad = @as(usize, if (text < 10) 3 else if (text < 100) 2 else 1);
... Which still fails with the same error.
I have to unfortunately case every single value instead: const left_pad = if (text < 10) @as(usize, 3) else if (text < 100) @as(usize, 2) else @as(usize, 1); Which is really awkward. Hope this kind of issue with comptime can be improved. |
Stage2 issue in #11812 |
Trying to compile
results in
I'm fairly new to Zig, so maybe I need to solve this differently and the error is intended behavior, but it feels like it shouldn't be.
The text was updated successfully, but these errors were encountered: