I am working and learning Dictionaries, now here is what is happening,

```
# Initialize a dictionary
d = Dict("a"=>1, "b" => 2, "c" => 3)
d["c"] = 4 # works
d["c"] = "Hello" # Fails with error pasted below
d["d"] = "Hello" # Fails with same error
d["d"] = 9 # works
```

Error is

MethodError: Cannot `convert`

an object of type String to an object of type Int64

Closest candidates are:

convert(::Type{T}, !Matched::T) where T<:Number

@ Base number.jl:6

convert(::Type{T}, !Matched::Number) where T<:Number

@ Base number.jl:7

convert(::Type{T}, !Matched::Base.TwicePrecision) where T<:Number

@ Base twiceprecision.jl:273

…

**setindex!**(::Dict{String, Int64}, ::String, ::String)@*dict.jl:369*
**top-level scope**@*Local: 1* [inlined]

lmiq
2
When you initialize your dict like the above, the Dict created becomes specific for string keys and int values:

```
julia> d = Dict("a"=>1, "b" => 2, "c" => 3)
Dict{String, Int64} with 3 entries:
"c" => 3
"b" => 2
"a" => 1
```

(note the `Dict{String, Int64}`

).

If you want the Dict to contain more general values than those used to initialize the dict, you can be explicit about that, for example:

```
julia> d = Dict{String,Union{String,Int}}("a"=>1, "b" => 2, "c" => 3)
Dict{String, Union{Int64, String}} with 3 entries:
"c" => 3
"b" => 2
"a" => 1
julia> d["c"] = "a"
"a"
julia> d
Dict{String, Union{Int64, String}} with 3 entries:
"c" => "a"
"b" => 2
"a" => 1
```

or, even more general:

```
julia> d = Dict{String,Any}("a"=>1, "b" => 2, "c" => 3)
Dict{String, Any} with 3 entries:
"c" => 3
"b" => 2
"a" => 1
julia> d["c"] = [1,2,3]
3-element Vector{Int64}:
1
2
3
julia> d
Dict{String, Any} with 3 entries:
"c" => [1, 2, 3]
"b" => 2
"a" => 1
```

6 Likes