#Should I get rid of this global? (and if so, how?)

1 messages · Page 1 of 1 (latest)

cedar stream
#

context: I'm experimenting with creating a dependency-less pure-zig windowing library, and I store which backend is in use (X11, wayland, win32...) as a global within the main file of the library
I'm not sure why, but it feels like I shouldn't do this as a global? but the only way I can think of to make it not a global would be to turn this into a file struct and return it from init() but that could be inconvenient for users of the library. thoughts on this option?
otherwise, what other options are there? or do I just keep it the way it is?
(note: code & interface is highly likely to change)
(// ... used to indicate there's code that hasn't been included due to not being relevant)
relevant code:

// ...
var used_backend: backend.Backend = undefined;

/// init the library backend
pub fn init(allocator: std.mem.Allocator) !void {
    used_backend = try backend.Backend.init(allocator);
}

/// deinit everything
pub fn deinit() void {
    used_backend.vtable.deinit(used_backend.ptr);
}
// ...

/// represents a created window
pub const Window = struct{
    backend: backend.BackendWindow,

    /// create and open a window
    pub fn create(allocator: std.mem.Allocator, options: WindowOptions) !Window {
        return Window{
            .backend = try used_backend.vtable.openWindow(used_backend.ptr, allocator, options),
        };
    }

    /// destroys an opened window
    pub fn destroy(self: Window) void {
        used_backend.vtable.closeWindow(used_backend.ptr, self.backend.ptr);
    }
    // ...
};
// ...
proper carbon
#

is the value a singleton? should there ever be more than one instance of it? if not, I don't see an issue with it being a global...
std.os.argv is just fine being a global - the program gets its arguments once