Hi, just started learning zig (v0.13) and this is my first post here.
I was wondering about title and upon searching for it I only found answers for generic writes, not buffered.
I’ve tried a bunch of things but ran out of ideas:
const std = @import("std");
// fn usage(buffer: std.fs.File.Writer) !void {
// fn usage(buffer: std.io.buffered_writer.BufferedWriter) !void {
// fn usage(buffer: std.io.BufferedWriter) !void {
// fn usage(buffer: std.io.BufferedWriter.Writer) !void {
// fn usage(buffer: std.io.Writer) !void {
fn usage(buffer: anytype) !void {
try buffer.writeAll("lol");
}
pub fn main() !void {
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try usage(stdout);
try bw.flush();
}
So, anytype Does work, but im not entirely happy with it
Doing
fn usage(buffer: std.fs.File.Writer) !void {
Gives: (aside: can i wrap text in preformatted?)
error: expected type 'io.GenericWriter(fs.File,error{DiskQuota,FileTooBig,InputOutput,NoSpaceLeft,DeviceBusy,InvalidArgument,AccessDenied,BrokenPipe,SystemResources,OperationAborted,NotOpenForWriting,LockViolation,WouldBlock,ConnectionResetByPeer,Unexpected},(function 'write'))', found 'io.GenericWriter(*io.buffered_writer.BufferedWriter(4096,io.GenericWriter(fs.File,error{DiskQuota,FileTooBig,InputOutput,NoSpaceLeft,DeviceBusy,InvalidArgument,AccessDenied,BrokenPipe,SystemResources,OperationAborted,NotOpenForWriting,LockViolation,WouldBlock,ConnectionResetByPeer,Unexpected},(function 'write'))),error{DiskQuota,FileTooBig,InputOutput,NoSpaceLeft,DeviceBusy,InvalidArgument,AccessDenied,BrokenPipe,SystemResources,OperationAborted,NotOpenForWriting,LockViolation,WouldBlock,ConnectionResetByPeer,Unexpected},(function 'write'))'
Ok, seems it should be io.GenericWriter(*io.buffered_writer.BufferedWriter…
fn usage(buffer: std.io.buffered_writer.BufferedWriter) !void {
And with that
error: root struct of file 'io' has no member named 'buffered_writer'
Oh, lets remove that
fn usage(buffer: std.io.BufferedWriter) !void {
But
error: expected type 'type', found 'fn (comptime usize, comptime type) type'
So it seems thats a function, not a type? The docs Zig Documentation say Type Function. Dont fully know what that means but i can see it making sense. In the same docs, under Types, it list Writer so lets try with that
fn usage(buffer: std.io.BufferedWriter.Writer) !void {
gives
error: type 'fn (comptime usize, comptime type) type' does not support field access
And trying a more generic approach?
fn usage(buffer: std.io.Writer) !void {
gives
error: expected type 'type', found 'fn (comptime type, comptime type, comptime anytype) type'
And thats it. Cant think what to try next. Thanks in advance