#catch/switch - error: incompatible types: 'fs.File' and 'void'

1 messages · Page 1 of 1 (latest)

lofty yew
#

I want to open a file, if that file doesnt exist, I want to create it. So im attempting to open, catch the FileNotFound and create it.

#
const std = @import("std");

pub fn main() !void {
    std.log.info("Starting Up\n", .{});
    var database_file = std.fs.cwd().openFile("zig.db", .{}) catch |err| switch (err) {
        error.FileNotFound => {
            std.log.err("File Not Found\n", .{});
        },
        else => {
            std.debug.panic("Panicked at Error: {any}", .{err});
        },
    };
    defer database_file.close();
}
#
❯ zig build run
run
└─ run zigdb
   └─ zig build-exe zigdb Debug native 1 errors
src/main.zig:12:62: error: incompatible types: 'fs.File' and 'void'
    var database_file = std.fs.cwd().openFile("zig.db", .{}) catch |err| switch (err) {
                                                             ^~~~~
src/main.zig:12:62: note: type 'fs.File' here
src/main.zig:13:31: note: type 'void' here
        error.FileNotFound => {
                              ^
referenced by:
    callMain: /home/justin/zig/0.13.0/files/lib/std/start.zig:524:32
    callMainWithArgs: /home/justin/zig/0.13.0/files/lib/std/start.zig:482:12
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: the following command failed with 1 compilation errors:
/home/justin/zig/0.13.0/files/zig build-exe -ODebug -Mroot=/home/justin/repos/zigdb/src/main.zig --cache-dir /home/justin/repos/zigdb/.zig-cache --global-cache-dir /home/justin/.cache/zig --name zigdb --listen=- 
Build Summary: 0/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run zigdb transitive failure
   ├─ zig build-exe zigdb Debug native 1 errors
   └─ install transitive failure
      └─ install zigdb transitive failure
         └─ zig build-exe zigdb Debug native (reused)
error: the following build command failed with exit code 1:
/home/justin/repos/zigdb/.zig-cache/o/b4c67704291554e28aa08115e20e91c9/build /home/justin/zig/0.13.0/files/zig /home/justin/repos/zigdb /home/justin/repos/zigdb/.zig-cache /home/justin/.cache/zig --seed 0x37c51abc -Zc9fafff51dee1d36 run
solid fable
#

the catch branch has to return something of the same type

#
const a: i32 = some_errorable() catch 25; // if there is an error, a is 25

you probably want to return if there's an error I assume?

const a: i32 = some_errorable catch {
    return;
};
solid fable
# lofty yew ``` const std = @import("std"); pub fn main() !void { std.log.info("Startin...

the reason this doesn't work is because the catch branch is returning void

// this doesn't work
const file = openFile() catch {
    std.log.err("File not found", .{});
    // (code keeps executing after this point)
};
print(file); // if the file isn't found, what would this do?

with catch, you need to provide a value for the error case

const my_number = makeNumber() catch 25;
print(my_number); // if makeNumber fails, my_number is 25

or you need to return so the code doesn't continue beyond the point

const file = openFile() catch {
    std.log.err("File not found", .{});
    return;
};
print(file); // if the file isn't found, we have returned from the function so the code will never get here
lofty yew
lofty yew
lofty yew
#

Do I need to return a fs.File in each section of that switch?

carmine summit