Why can't I append a Date to a list?

Trying to append a Date or DateTime instance to a list results in a MethodError:

julia> v = []
0-element Array{Any,1}

julia> myDate = Date(2018,2,12)
2018-02-12

julia> append!(v,myDate)
ERROR: MethodError: no method matching length(::Date)
Closest candidates are:
length(::SimpleVector) at essentials.jl:256
length(::Base.MethodList) at reflection.jl:558
length(::MethodTable) at reflection.jl:634

Why is this, and how can one generate a dynamically-sized array of Dates with this limitation?

1 answer

  • answered 2018-02-13 01:08 Picaud Vincent

    From the doc (?push! and ?appned!) you must use push! to add an individual element. append! applies to collections

    julia> v = []
    0-element Array{Any,1}
    
    julia> myDate = Date(2018,2,12)
    2018-02-12
    
    julia> push!(v,myDate)
    1-element Array{Any,1}:
     2018-02-12
    
    julia> append!(v,[myDate myDate myDate])
    4-element Array{Any,1}:
     2018-02-12
     2018-02-12
     2018-02-12
     2018-02-12