Working:
const std = @import("std");
const Mode = enum {
Low, Medium, High, Custom
};
pub fn main() void {
var xoroshiro = std.rand.Xoroshiro128.init(std.time.milliTimestamp());
var random = xoroshiro.random;
const mode: Mode = switch (random.float(f32)) {
0.0...0.2 => .Low,
0.2...0.4 => .Medium,
0.4...0.6 => .High,
0.6...1.0 => .Custom,
else => unreachable,
};
std.debug.warn("{}\n", .{mode});
}
Not working:
const std = @import("std");
const Mode = enum {
Low, Medium, High, Custom
};
pub fn main() void {
var xoroshiro = std.rand.Xoroshiro128.init(std.time.milliTimestamp());
var random = xoroshiro.random;
const i: usize = 5;
const mode: Mode = switch (i) {
0 => .Custom,
else => blk: {
break :blk switch (random.float(f32)) {
0.00...0.33 => .Low,
0.33...0.66 => .Medium,
0.66...1.00 => .High,
else => unreachable
};
}
};
std.debug.warn("{}\n", .{mode});
}
Something wrong with inferring types and nested switches, maybe?
The type inferencer has a hard time with nested expressions:
Just confirming, this is a bug. It's fixed in stage 2.
Most helpful comment
Just confirming, this is a bug. It's fixed in stage 2.