#Accessing std.Io instance in deep functions
1 messages · Page 1 of 1 (latest)
either pass it deeply or (in application/non-library code only) you could use global vars. the former approach is more friendly for unit tests though
although, unit tests doing io might be weird regardless
can you give an example for using "global vars" ?
something like this, its a very bad practice to do if this is library logic though since the application can no longer control the io instance.
the "correct" answer is yes pass it as a argument to each function
const std = @import("std");
var IoImpl: std.Io = undefined;
pub fn main() !void {
const alloc = std.heap.smp_allocator;
var threaded = std.Io.Threaded.init(alloc, .{});
IoImpl = threaded.io();
_ = IoImpl;
}
fn otherFunc() !void {
_ = IoImpl; // used here!
}