Yesterday I spent a long time trying to declare a bufferedReader. I eventually got it but I'd like to understand what is going on.
First question:
This works:
var stdin_buf_ = std.io.bufferedReader(std.io.getStdIn().reader());
var stdin_buf = stdin_buf_.reader();
This doesn't work:
var stdin_buf = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
The error I get is basically: expected type "*io.buffered_reader...", found "*const io.buffered_reader...".
I'm guessing what's going on is basically that a pointer is being created in bufferedReader and since we don't explicitly store it in a variable it is const by default?
Is there some other notation I could use or is my first code the way to do it?
Second Question
What is much more confusing is why the following doesn't work:
var stdin = std.io.getStdIn().reader();
var stdin_buf = std.io.bufferedReader(&stdin).reader();
I get the error:
/usr/lib/zig/std/io/buffered_reader.zig:14:37: error: type '*io.GenericReader(fs.File,error{InputOutput,SystemResources,IsDir,OperationAborted,BrokenPipe,ConnectionResetByPeer,ConnectionTimedOut,NotOpenForReading,SocketNotConnected,WouldBlock,AccessDenied,Unexpected},(function 'read'))' has no members
pub const Error = ReaderType.Error;
The BufferedReader "template" defines its "Error" field to be the "Error" field of the the type of the reader you passed into it
But I don't get how just putting the stdin outside of the function removes "Error" from it. How are the two declarations any different? Is there some hidden cast going at some point?