Julia: Array allocation happens before type checking

Created on 19 Jun 2020  路  4Comments  路  Source: JuliaLang/julia

Julia v1.3.0 on OSX v10.15.4

a = Array[]

append!(a, "foo")
> ERROR: MethodError: Cannot `convert` an object of type Char to an object of type String

push!(a, "bar")
> 4-element Array{String,1}:
 #undef  
 #undef  
 #undef  
    "bar"

Most helpful comment

Duplicate of https://github.com/JuliaLang/julia/issues/15868 and https://github.com/JuliaLang/julia/issues/7642, ref https://github.com/JuliaLang/julia/pull/7671.

Yes, this is annoying behaviour, but checking whether each element can convert is unacceptably slow.

All 4 comments

Would that do what you expect?

append!(a, iter) = for i in iter; push!(a, i) end

Duplicate of https://github.com/JuliaLang/julia/issues/15868 and https://github.com/JuliaLang/julia/issues/7642, ref https://github.com/JuliaLang/julia/pull/7671.

Yes, this is annoying behaviour, but checking whether each element can convert is unacceptably slow.

Would that do what you expect?

append!(a, iter) = for i in iter; push!(a, i) end

Yes it would. I was mainly surprised that a failed operation grew the array, this behaviour was unexpected.

Duplicate of #15868 and #7642, ref #7671.

Yes, this is annoying behaviour, but checking whether each element can convert is unacceptably slow.

That's fair! I'm closing this issue in favour of #15868.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iamed2 picture iamed2  路  3Comments

manor picture manor  路  3Comments

felixrehren picture felixrehren  路  3Comments

dpsanders picture dpsanders  路  3Comments

tkoolen picture tkoolen  路  3Comments