You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// b.zigpubfnmain() void {
constz=scale(4, f32, 1.75);
@import("std").log.debug("z={}", .{z});
}
fnscale(n: i32, comptimeFactor: type, factor: Factor) Factor {
// first attempt was:// return @intCast(i32, @as(Factor, n) * factor)// attempting to split it up like so results in the same errorvarnew=@as(Factor, n);
new*=factor;
return@as(i32, new);
}
Expected Behavior
Both of the above programs to compile and run, logging the same value for z
Actual Behavior
a.zig runs fine, b.zig has a compile error:
$ zig run b.zig
.\b.zig:11:27: error: expected type 'f32', found 'i32'
var new = @as(Factor, n);
^
.\b.zig:3:20: note: called from here
const z = scale(4, f32, 1.75);
^
.\b.zig:2:20: note: called from here
pub fn main() void {
^
After messing around, I think it could have something to do with the fact that @as doesn't like certain float to int conversions (hence the existence of @intToFloat and @floatToInt), and the error reporting gets confused and reports this as expected <destination type>, found <actual argument type> rather than something like unable to cast value of type <type>, to type <type>.
I've adapted the actual program to check at compile time whether Factor is integral or floating point and done the appropriate @intToFloat/@floatToInt, but this error message through me for a loop
The text was updated successfully, but these errors were encountered:
Is it defined that the compiler will allow any int<->float conversion with @as when they're comptime-known to be safe? If not, it's actually a bug that a.zig compiles here. (But yes the error message could definitely be improved)
Zig Version
0.10.0-dev.74+426679574
Steps to Reproduce
try to
zig run
the following programsExpected Behavior
Both of the above programs to compile and run, logging the same value for
z
Actual Behavior
a.zig runs fine, b.zig has a compile error:
After messing around, I think it could have something to do with the fact that
@as
doesn't like certain float to int conversions (hence the existence of@intToFloat
and@floatToInt
), and the error reporting gets confused and reports this asexpected <destination type>, found <actual argument type>
rather than something likeunable to cast value of type <type>, to type <type>
.I've adapted the actual program to check at compile time whether
Factor
is integral or floating point and done the appropriate@intToFloat
/@floatToInt
, but this error message through me for a loopThe text was updated successfully, but these errors were encountered: