#Switch branch syntax with optional

1 messages · Page 1 of 1 (latest)

stone copper
#

I'm trying to get a switch branch working if an optional is equal to a specific value but I'm new and recently learning similar but different languages. Can't figure it out from the docs because I'm doing multiple new syntaxes at once

./src/main.zig:167:22: error: values of type 'comptime_int' must be comptime known
            .File => if (optsize != null and optsize.? == 0) '0' else ' ',
#

what is/are the comptime ints and why aren't they known? is it the '0' and ' '? -:

thorny matrix
#

there is no way to answer without seeing the context, please show more code. the type of that expression depends on what is being assigned the value of the switch.

stone copper
#
        const kindSymbol: u8 = switch (entry.kind) {
            .File => if (optsize != null and optsize.? == 0) '0' else ' ',
            .Directory => '/',
            .SymLink => '~',
            else => '?',
        };
        std.debug.print("{c} {s}\n", .{kindSymbol, entry.name});
#

ah i fixed it by adding @as(u8, '0')

thorny matrix
#

glad you fixed it. that looks like older behavior, are you using an older version of zig?

gaunt tartan
#

I think it's because the if evaluates to a comptime_int, but depends on runtime values
the error messages are sometimes a bit cryptic

#

but then not sure why that isn't the case for the switch...

thorny matrix
#

i don't get that error on recent versions of zig, that's why I ask

stone copper
#

i'm on 0.10.0-dev.3475+b3d463c9e because i don't have the hang of everything i have to change it takes time to update including the vscode extensions and all

thorny matrix
#

that's more recent than i tried! 🙂 maybe there was a regression

stone copper
#

i have a newer one downloaded but last couple of upgrades took a while to track everything down. i'm not even used to setting up paths on macos yet

thorny matrix
#

yeah, strange, i don't get that error on the latest master on the download page:
0.10.0-dev.4217+9d8cdb855

woven idol
#

feel free to submit a bug for that

#

the : u8 in the variable definition should be enough to silence that error