#Accessing std.Io instance in deep functions

1 messages · Page 1 of 1 (latest)

compact ore
#

How can i access std.Io instance in deep functions. I know that i can access via init.io in main function. but if i need to access std.Io instance in deep functions, should I need to pass it to every function i call? furthermore this also applies to other instances in std.process.Init

#

Accessing std.Io instance in deep functions

tropic mortar
#

although, unit tests doing io might be weird regardless

compact ore
nocturne tinsel
#

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!
}