# Cannot assign struct as a dictionary value

**URL:** https://discourse.julialang.org/t/cannot-assign-struct-as-a-dictionary-value/54027
**Category:** New to Julia
**Created:** [January 27, 2021, 2:38am UTC](https://discourse.julialang.org/t/cannot-assign-struct-as-a-dictionary-value/54027 "2021-01-27T02:38:59Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ruysan](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@ruysan](https://discourse.julialang.org/u/ruysan)
#### Post date: [January 27, 2021, 2:38am UTC](https://discourse.julialang.org/t/cannot-assign-struct-as-a-dictionary-value/54027/1 "2021-01-27T02:38:59Z")

</div>

Hi!  
I have a simple struct to store some data:

```julia
struct Gene
    name::String
    exonarray::Array
    pi::Int64
    pf::Int64
    is_reverse::Bool
    #note::String 
end

```

I then get some data from a file and generate an object:

```julia
myname = "Gene_1"
myarray = []
mypi = 1
mypf = 1500
myrev = false

mygene = Gene(myname,myarray,mypi,mypf,myrev)

```

Then, I want to push this new object into a dictionary, using a string as key.

```julia
mydict = Dict
mydict[myname] = mygene

```

This raises an error:

```julia
ERROR: LoadError: MethodError: no method matching setindex!(::Type{Dict}, ::Gene, ::String)

```

What am I doing wrong? How do I assign a struct as a Dictionary value?

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [January 27, 2021, 2:50am UTC](https://discourse.julialang.org/t/cannot-assign-struct-as-a-dictionary-value/54027/2 "2021-01-27T02:50:46Z")

</div>

> [@ruysan](#):
>
> `mydict = Dict`

Change that to `mydict = Dict()`.

Or even better:

```julia
mydict = Dict{String, Gene}()

```

---

<div class="post-metadata">

### Author: ![ruysan](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@ruysan](https://discourse.julialang.org/u/ruysan)
#### Post date: [January 27, 2021, 4:50am UTC](https://discourse.julialang.org/t/cannot-assign-struct-as-a-dictionary-value/54027/3 "2021-01-27T04:50:35Z")

</div>

if I try

```julia
mydict = Dict()
#or
mydict = Dict{String,Gene}()

```

I get a slightly different error:

```julia
ERROR: LoadError: MethodError: no method matching getindex(::Gene, ::Int64)

```

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [January 27, 2021, 6:30am UTC](https://discourse.julialang.org/t/cannot-assign-struct-as-a-dictionary-value/54027/4 "2021-01-27T06:30:24Z")

</div>

What code are you running to get this error? The code you posted above works with either `mydict = Dict()` or `mydict = Dict{String,Gene}()`:

```julia
julia> struct Gene
           name::String
           exonarray::Array
           pi::Int64
           pf::Int64
           is_reverse::Bool
           #note::String
       end

julia> myname = "Gene_1"
"Gene_1"

julia> myarray = []
Any[]

julia> mypi = 1
1

julia> mypf = 1500
1500

julia> myrev = false
false

julia> mygene = Gene(myname,myarray,mypi,mypf,myrev)
Gene("Gene_1", Any[], 1, 1500, false)

julia> mydict = Dict{String,Gene}()
Dict{String,Gene}()

julia> mydict[myname] = mygene
Gene("Gene_1", Any[], 1, 1500, false)

julia> mydict
Dict{String,Gene} with 1 entry:
  "Gene_1" => Gene("Gene_1", Any[], 1, 1500, false)

```

---

<div class="post-metadata">

### Author: ![ruysan](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@ruysan](https://discourse.julialang.org/u/ruysan)
#### Post date: [January 27, 2021, 7:21am UTC](https://discourse.julialang.org/t/cannot-assign-struct-as-a-dictionary-value/54027/5 "2021-01-27T07:21:36Z")

</div>

Big confusion. I modified the variable names a bit, (like my\_dict to mydict, and my\_gene to mygene).  
Now the code works as it should. Thanks for your help!  
What is the effect of the ()? What is the difference between Dict and Dict()?

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [January 27, 2021, 7:48am UTC](https://discourse.julialang.org/t/cannot-assign-struct-as-a-dictionary-value/54027/6 "2021-01-27T07:48:02Z")

</div>

`Dict` is a type and `Dict()` creates an object of that type (in this case, of the type `Dict{Any,Any}`, which is a concrete subtype of `Dict`). It’s like the difference between `Int64` (a type) and `5` (a value of that type).

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [January 27, 2021, 7:59am UTC](https://discourse.julialang.org/t/cannot-assign-struct-as-a-dictionary-value/54027/7 "2021-01-27T07:59:10Z")

</div>

> [@ruysan](#):
>
> ```julia
> struct Gene
> name::String
> exonarray::Array
> pi::Int64
> pf::Int64
> is_reverse::Bool
> #note::String 
> end
> 
> ```

For better performance, you should pick a concrete type for the `exonarray` field, like `Array{Int64,1}` instead of `Array`.
