I have some code that uses a switch
to call function A
or B
based on some input, and a variable to capture the output from the switch
. The functions can error, so they’re called with try
.
Here’s a minimal example:
const std = @import("std");
const SomeError = error{TheError};
const Orientation = enum { Horizontal, Vertical };
fn dontProvideZero(input: u8) !u8 {
if (input == 0) {
return SomeError.TheError;
} else {
return input;
}
}
fn dontProvideOne(input: u8) !u8 {
if (input == 1) {
return SomeError.TheError;
} else {
return input;
}
}
pub fn main() !void {
const input = 5;
const orientation = Orientation.Horizontal;
const output = switch (orientation) {
.Horizontal => {
try dontProvideZero(input);
},
.Vertical => {
try dontProvideOne(input);
},
};
_ = output;
}
When I compile this, I get:
scratch.zig:27:13: error: value of type 'u8' ignored
try dontProvideZero(input);
^~~~~~~~~~~~~~~~~~~~~~~~~~
scratch.zig:27:13: note: all non-void values must be used
scratch.zig:27:13: note: to discard the value, assign it to '_'
referenced by:
main: /opt/homebrew/Cellar/zig/0.14.1/lib/zig/std/start.zig:660:37
comptime: /opt/homebrew/Cellar/zig/0.14.1/lib/zig/std/start.zig:58:30
2 reference(s) hidden; use '-freference-trace=4' to see all references
And I don’t understand because I am using the output below the switch. What am I missing?