# Using custom types in OrderedDict

**URL:** <https://discourse.julialang.org/t/using-custom-types-in-ordereddict/24183>\
**Category:** New to Julia\
**Created:** [May 13, 2019, 5:41pm UTC](https://discourse.julialang.org/t/using-custom-types-in-ordereddict/24183 "2019-05-13T17:41:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgalenb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgalenb/32/5645_2.png) [@mgalenb](https://discourse.julialang.org/u/mgalenb)\
**Post date:** [May 13, 2019, 5:41pm UTC](https://discourse.julialang.org/t/using-custom-types-in-ordereddict/24183/1 "2019-05-13T17:41:31Z")

</div>

I am writing a code that reads a binary file and populates an OrderedDict. I have custom types that i made such as:

```julia
mutable struct WBGAIN
	R::Float32
	B::Float32
end

```

when i try to do something like this:

```julia
using DataStructures: OrderedDict
function readheader(fname)
    h = OrderedDict()

        fhtypes = OrderedDict( # File header
            :HeadSize => UInt16,
            :Compression => UInt16,
            :Version => UInt16,
            :WBgain1 => WBGAIN)
end

```

I get an error saying that the type WBGAIN is not supported by OrderedDict. Is there a way to put custom types into an OrderedDict?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 13, 2019, 6:38pm UTC](https://discourse.julialang.org/t/using-custom-types-in-ordereddict/24183/2 "2019-05-13T18:38:17Z")

</div>

I’m not able to reproduce this error, could you show the stack trace of the error explicitly?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 13, 2019, 6:43pm UTC](https://discourse.julialang.org/t/using-custom-types-in-ordereddict/24183/3 "2019-05-13T18:43:11Z")

</div>

There is a distinction between the types of dictionary values and valuing dictionary keys.  
Initialize your dictionary with typed values rather than types:

```julia
using DataStructures: OrderedDict

mutable struct MyStruct
    value1::Float32
    value2::Float32
end

myodict = OrderedDict( :HeadSize => UInt16(1), 
                       :MyStruct => MyStruct(0.0f0, 0.0f0) )

# OrderedDict{Symbol,Any} with 2 entries:
# :HeadSize => 0x0001
# :MyStruct => MyStruct(0.0, 0.0)

myodict[:MyStruct] = MyStruct(1.0f0, 2.0f0)
# MyStruct(1.0f0, 2.0f0)

myodict
# OrderedDict{Symbol,Any} with 2 entries:
# :HeadSize => 0x0001
# :MyStruct => MyStruct(1.0, 2.0)

```
