I’m relatively new to coding and I don’t have a CS or SE degree.
I’ve written a program in C, then Rust and now I’m trying to write it in Zig.
I really like how explicit you have to be with everything in Zig and it’s making me feel like I understand more about what the code is actually doing.
However, I’ve been running into a lot of issues and I’m having trouble understanding the compiler.
For this bit of the code, I basically want a function which I can use later to get input from the user and handle any errors.
(I plan to do another function for string input later).
Eventually the getUserInput() function will be used to navigate menus and enter in numbers for calculations.
I think I have a poor understanding of error unions but maybe it’s more than that.
I’m sure this is a simple problem.
But I’m at the point where I need to ask for help.
How do I fix this? Also, what should I work on understanding for not being able to fix this myself.
Here is an example of the code:
I’m having a lot of trouble posting on this forum.
I’m pretty sure those arrows didn’t print out correctly.
~/code/zig/ditmco2aas: zig run ditmco2aas.zig
ditmco2aas.zig:20:20: error: function with non-void return type 'typeInfo(typeInfo(TypeOf
(ditmco2aas.getUserInput)).Fn.return_type.?).ErrorUnion.error_set!u8' implicitly returns
fn getUserInput() !u8 {
^~
ditmco2aas.zig:32:1: note: control flow reaches end of body here
}
^
referenced by:
main: ditmco2aas.zig:38:28
callMain: /usr/lib/zig/std/start.zig:524:32
remaining reference traces hidden; use '-freference-trace' to see all reference traces
/usr/lib/zig/std/fmt.zig:521:17: error: cannot format error union without a specifier (i.e.
{!} or {any})
compileError("cannot format error union without a specifier (i.e. {!} or {a
ny})");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~
Interesting.
Return err; gives me an LSP error "use of undeclared identifier ‘err’ ".
If I try " return 0" I get a compiler error.
~/code/zig/ditmco2aas: zig run ditmco2aas.zig
/usr/lib/zig/std/fmt.zig:521:17: error: cannot format error union without a specifier (i.e.
{!} or {any})
@compileError("cannot format error union without a specifier (i.e. {!} or {a
ny})");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~
referenced by:
format__anon_4002: /usr/lib/zig/std/fmt.zig:185:23
print__anon_3623: /usr/lib/zig/std/io/Writer.zig:24:26
remaining reference traces hidden; use '-freference-trace' to see all reference traces
~/code/zig/ditmco2aas:
I believe I get the same compiler error for trying "return error.InvalidCharacter.
Thanks I didn’t realize the difference in syntax for some of your postings. I’m on a small laptop unfortunately.
It’s working now!
But I definitely need to understand errors better.
I’ll most likely be able to reproduce the program I’ve written in C and rust now.
Do you have any suggestions?
Also, will returning the error always crash the program?
If so, I may need to return -1. (0 or positive returns will be problematic)