Skip to content

values of type '(enum literal)' must be comptime known #4629

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

Closed
Sobeston opened this issue Mar 4, 2020 · 3 comments
Closed

values of type '(enum literal)' must be comptime known #4629

Sobeston opened this issue Mar 4, 2020 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@Sobeston
Copy link
Contributor

Sobeston commented Mar 4, 2020

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?

@Sobeston Sobeston changed the title cannot store runtime value in type '(enum literal)' values of type '(enum literal)' must be comptime known Mar 4, 2020
@fengb
Copy link
Contributor

fengb commented Mar 5, 2020

@SpexGuy
Copy link
Contributor

SpexGuy commented Dec 8, 2020

Just confirming, this is a bug. It's fixed in stage 2.

@SpexGuy SpexGuy added the bug Observed behavior contradicts documented or intended behavior label Dec 8, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@Sobeston
Copy link
Contributor Author

Updating the code so that it builds with the latest version of zig

const std = @import("std");

const Mode = enum { Low, Medium, High, Custom };

pub fn main() void {
    var xoroshiro = std.rand.Xoroshiro128.init(@intCast(std.time.milliTimestamp()));
    var random = xoroshiro.random();

    const i: usize = 5;
    const mode: Mode = switch (i) {
        0 => .Custom,
        else => blk: {
            break :blk switch (random.intRangeAtMost(u8, 0, 100)) {
                0...33 => .Low,
                34...66 => .Medium,
                67...100 => .High,
                else => unreachable,
            };
        },
    };

    std.debug.print("{}\n", .{mode});
}

This now works, going ahead and closing the issue.

@Vexu Vexu modified the milestones: 0.15.0, 0.12.0 Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

5 participants