Issue while using dictionary in Julia

items = Dict{String,Int64}
Total_craneMovement =985
MaxCraneMovement =312
 CraneIntensity =Total_craneMovement/MaxCraneMovement
push!(items,"Total_craneMovement"=>Total_craneMovement)
push!(items,"MaxCraneMovement"=>MaxCraneMovement)
push!(items,"CraneIntensity"=>CraneIntensity)

MethodError: no method matching push!(::Type{Dict{String, Float64}}, ::Pair{String, Int64})
Closest candidates are:
push!(::Any, ::Any, !Matched::Any) at abstractarray.jl:2387
push!(::Any, ::Any, !Matched::Any, !Matched::Any…) at abstractarray.jl:2388
push!(!Matched::CategoricalArrays.CategoricalVector{T, R, V, C, U} where {T, R<:Integer, V, C, U}, ::Any) at C:\Users\manojkumar.ram.julia\packages\CategoricalArrays\rDwMt\src\array.jl:866
…
in eval at base\boot.jl:360
in top-level scope at untitled:5

This line makes items equal to the type Dict{String, Int64}; it doesn’t actually create a dictionary. You want Dict{String, Int64}() to actually construct a new Dict.

6 Likes

This mistake is so common, that it probably deserve it’s own error message. Something like

push!(::Type{Dict}, whatever...) = error("You are trying to push in a type, maybe you wanted to instantiate your variable with Dict()? Pay special attention to brackets after Dict.")
2 Likes

That would be a really good pr (also a good first pr if someone is trying to start contributing).

Another possible improvement would be to change the metric for “closest candidates” so that replacing a type by its instance counts as very “close”. This would put push!(::AbstractDict, ::Pair)` at the top of the list in the current error message.