JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.93k stars 5.49k forks source link

no method +(Int64, Nothing) when writing strings that have been split #6949

Closed swadey closed 10 years ago

swadey commented 10 years ago

When writing out to a file, I get:

ERROR: no method +(Int64, Nothing)
 in write at io.jl:61
 in write at io.jl:391
 in include at boot.jl:244
 in include_from_node1 at loading.jl:128
while loading /Users/swade/projects/julia/test.jl, in expression starting on line 10

Here's a minimized code example:

names = (String)[]

inf = open("test.jl")
for i in eachline(inf)
  push!(names, split(i, r"\s+")[1]) # NOTE: split call, doesn't raise error without it
end
close(inf)

f = open("test.bin", "w")
write(f, names)
close(f)
swadey commented 10 years ago

Seems to be related to writing of SubString

ihnorton commented 10 years ago

How so? We've seen some other SubString weirdness recently so if you have an example/test case it might be helpful.

Keno commented 10 years ago

Well, it seems quite clear that one of the write methods is not returning the number of bytes written as it should.

swadey commented 10 years ago

OK, here's a simpler example:

julia> a = ["1", "2", "3"]
3-element Array{ASCIIString,1}:
 "1"
 "2"
 "3"

julia> open("test.out", "w") do f write(f, a) end
3

julia> a = split(strip("1 2 3"), r"\s+")
3-element Array{SubString{ASCIIString},1}:
 "1"
 "2"
 "3"

julia> open("test.out", "w") do f write(f, a) end
ERROR: no method +(Int64, Nothing)
 in write at io.jl:61
 in write at io.jl:391
 in anonymous at none:1
 in open at io.jl:373

julia> open("test.out", "w") do f x = write(f, a[1]::SubString{ASCIIString}); println(x) end
nothing

I'm guessing that this is an invariance problem, i.e. SubString{ASCIIString} isn't the same as SubString{ByteString}?

kmsquire commented 10 years ago

Good call, @loladiro: https://github.com/JuliaLang/julia/blob/master/base/string.jl#L68