Julia: write(STDOUT,IOBuffer("a")) == nothing (should be 1)

Created on 4 Jan 2018  路  2Comments  路  Source: JuliaLang/julia

The docs for write say that it will return the number of bytes written to a stream. That means write(STDOUT,IOBuffer("a")) should return 1, instead it returns nothing.

Most helpful comment

diff --git a/base/io.jl b/base/io.jl
index ae71210cb1..06400023d1 100644
--- a/base/io.jl
+++ b/base/io.jl
@@ -565,9 +565,11 @@ function write(io::IO, s::Symbol)
 end

 function write(to::IO, from::IO)
+    i = 0
     while !eof(from)
-        write(to, readavailable(from))
+        i += write(to, readavailable(from))
     end
+    return i
 end

?

All 2 comments

The definition for this method is here: https://github.com/JuliaLang/julia/blob/master/base/io.jl#L567-L571

Loops return nothing and that's the last thing in the body.

diff --git a/base/io.jl b/base/io.jl
index ae71210cb1..06400023d1 100644
--- a/base/io.jl
+++ b/base/io.jl
@@ -565,9 +565,11 @@ function write(io::IO, s::Symbol)
 end

 function write(to::IO, from::IO)
+    i = 0
     while !eof(from)
-        write(to, readavailable(from))
+        i += write(to, readavailable(from))
     end
+    return i
 end

?

Was this page helpful?
0 / 5 - 0 ratings