Stuck with this! function type cannot have an inferred error set

trying to have some base code setup:

const std = @import("std");

// Define the explicit error set
pub const ObjectStorageError = error{
    FileNotFound,
    AccessDenied,
    OutOfMemory,
    InvalidInput,
};

pub const ObjectStorage = struct {
    // Explicitly specify the error set for each function pointer
    putIfAbsentFn: fn (self: *ObjectStorage, name: []const u8, bytes: []const u8) ObjectStorageError!void,
    listPrefixFn: fn (self: *ObjectStorage, prefix: []const u8, allocator: *std.mem.Allocator) ObjectStorageError!std.ArrayList([]const u8),
    readFn: fn (self: *ObjectStorage, name: []const u8, allocator: *std.mem.Allocator) ObjectStorageError![]const u8,

    pub fn putIfAbsent(self: *ObjectStorage, name: []const u8, bytes: []const u8) ObjectStorageError!void {
        return self.putIfAbsentFn(self, name, bytes);
    }

    pub fn listPrefix(self: *ObjectStorage, prefix: []const u8, allocator: *std.mem.Allocator) ObjectStorageError!std.ArrayList([]const u8) {
        return self.listPrefixFn(self, prefix, allocator);
    }

    pub fn read(self: *ObjectStorage, name: []const u8, allocator: *std.mem.Allocator) ObjectStorageError![]const u8 {
        return self.readFn(self, name, allocator);
    }
};

but keep getting the error function type cannot have an inferred error set

tried doing anyerror too, but can’t seem to get this work.

Hello and welcome. I think you’re might need to provide the actual code that’s failing. This code looks fine.

It seems like maybe you’re trying to write something like this elsewhere:

pub const Foo = struct {
    fn1: fn () !void,
};

Is that perhaps the case?

1 Like

Hey Travis, I am actually not running it btw! my bad?! it’s just having this code is showing an error in vscode. a red mark near the error set declaration.

Maybe try running it. zls can (rarely in my experience) get out of sync and might need a restart.

it ran! sorry for being so dumb! thank you travis!

1 Like

You are not dumb if you are trying to learn.

Don’t give up, keep at it, zig is new and still changing a lot, but it is worth the effort!

3 Likes