#Declaring a bufferedReader, what is the different between these?

1 messages · Page 1 of 1 (latest)

scarlet drift
#

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?

sacred ridge
# scarlet drift Yesterday I spent a long time trying to declare a bufferedReader. I eventually g...
  • yes, temporaries are const
  • in your particular example, you couldn't even use stdin_buf correctly since you couldn't call flush as you'd need to call that on the temporary that you're throwing away
  • for the second question, you shouldn't be passing a pointer to stdin; std.io.bufferedReader(stdin) would work (the return of reader() is a struct that contains a pointer, you pass that struct around by value)
scarlet drift
# sacred ridge - yes, temporaries are const - ~~in your particular example, you couldn't even u...

Thanks a lot for the help!

  • Makes sense
  • I don't think BufferedReader has flush... Only BufferedWriter
  • Here is the code passing it by value:
    const stdin = std.io.getStdIn().reader();
    var stdin_buf = std.io.bufferedReader(stdin).reader();

I had tried it like this before I got an error complaining about some pointer being const and assumed it was stdin's fault
Now I understand it that it's complaining that about BufferedReader being const (since it's a temporary)

So changing to this works:

    const stdin = std.io.getStdIn().reader();
    var stdin_buf_ = std.io.bufferedReader(stdin);
    var stdin_buf = stdin_buf_.reader();

I was confused about the fact that std.io.getStdIn().reader(); works since it looks like there's a temporary there too, but I'm seeing that getStdIn() is a File that gets passed by value to reader, so I guess it's OK