Julia: undocumented 1 argument write

Created on 25 Jul 2016  路  5Comments  路  Source: JuliaLang/julia

There is a 1 argument write in both 0.4.6 and 0.5.0 that is not described in the docstring or in the online documentation.

O

Most helpful comment

Not clear that this is necessary actually; I'm not sure it's even a good idea to have a shortcut for writing binary data to STDOUT.

All 5 comments

Not clear that this is necessary actually; I'm not sure it's even a good idea to have a shortcut for writing binary data to STDOUT.

I agree. Not having a default output stream seems like a key difference between print and write.

There are still multiple single argument write methods in 0.7.0-DEV.3096, which result from methods which have a splatted 2nd argument.

julia> methods(write,Tuple{Any})
# 3 methods for generic function "write":
[1] write(filename::AbstractString, args...) in Base at io.jl:275
[2] write(io::IO, xs...) in Base at io.jl:488

The first of these causes write("a") to create a file called "a" in the current directory, which seems undesirable.

Perhaps we should define something like:

Base.write(::AbstractString) = error("write requires two arguments")
Base.write(::IO) = error("write requires two arguments")

Probably a good idea to fix. Also unlikely that fixing them will cause anyone grief, but still...

Good catch. I think the first one should get an extra argument:

write(filename::AbstractString, a1, args...) = open(io->write(io, a1, args...), filename, "w")

The second one could too, but we also might want to just deprecate vararg write (which just loops over the arguments and calls write on all of them).

Was this page helpful?
0 / 5 - 0 ratings