#does switch work on optional enum?

1 messages · Page 1 of 1 (latest)

grand flax
#

Google Gemmi kept insisting it would work. I kept getting compile error at the switch () line.

const Command = enum { Start, Stop };

fn processCommand(maybe_cmd: ?Command) void {
    switch (maybe_cmd) {
        null => { },
        Command.Start => {  },
        Command.Stop => {  },
    }
}

pub fn main() !void {
    const cmd = Command.Start;
    processCommand(cmd);
}
austere cedar
#

gemini can say it will work all it wants, but the compile error doesn't lie

swift island
#

You can do if (maybe_cmd) |cmd| switch (cmd) {

#

Else branch on the if would be for null

grand flax
#

I know. I have kept pointing it out to it it doesn't work. It kept insisting I'm wrong, on using an old version of the compiler, on typing the wrong code, etc, etc. It's the level of its confidence on its BS that's worrying.

grave bronze
#

ez solution stop using it