Comment by messe

Comment by messe 17 hours ago

10 replies

You will need to pass that for synchronous IO as well. All IO in the standard library is moving to the Io interface. Sync and async.

If I want to call a function that does asynchronous IO, I'll use:

   foo(io, ...);
If I want to call one that does synchronous IO, I'll write:

    foo(io, ...);
If I want to express that either one of the above can be run asynchronously if possible, I'll write:

    io.async(foo, .{ io, ... });
If I want to express that it must be run concurrently, then I'll write:

    try io.concurrent(foo, .{ io, ... });
Nowhere in the above do I distinguish whether or not foo does synchronous or asynchronous IO. I only mark that it does IO, by passing in a parameter of type std.Io.
tcfhgj 17 hours ago

what about non-io code?

  • messe 17 hours ago

    What about it? It gets called without an Io parameter. Same way that a function that doesn't allocate doesn't get an allocator.

    I feel like you're trying to set me up for a gotcha "see, zig does color functions because it distinguishes functions that do io and those that don't!".

    And yes, that's true. Zig, at least Zig code using std, will mark functions that do Io with an Io parameter. But surely you can see how that will lead to less of a split in the ecosystem compared to sync and async rust?

    • torginus 14 hours ago

      This creates the drill-down issue we see with React props where we have to pass objects around in the call chain just so that somewhere down the line we can use it.

      React gets around this with the context hook and which you can access implicitly if it has been injected at a higher level.

      Do you know if Zig supports something of the sort?

      • throwaway17_17 10 hours ago

        I think (and I’m not a Zig user at anything above a hobbyist level) based on what the developers have discussed publically:

        React has a ‘roughly’ functional slant to the way it does things and so needs to provide a special case ‘hook’ for a certain type of context object. Zig however is an imperative language that allows for global state (and mutable global state for that matter), which means that there is always a way to access global variable, no hook required. On the other hand, I am relatively certain (almost 100% to be honest) there can not be a context/IO , or any data/variable, passed into a function higher up the call stack and have that propagate to the lower level via implicit inclusion.

      • messe 14 hours ago

        It doesn't and likely never will.

        This has been a non-issue for years with Allocator. I fail to see why it will be a problem with IO.

    • tcfhgj 17 hours ago

      > But surely you can see how that will lead to less of a split in the ecosystem compared to sync and async rust?

      not yet