# Instantiate the type from a few strings

**URL:** <https://discourse.julialang.org/t/instantiate-the-type-from-a-few-strings/10260>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 10, 2018, 8:37am UTC](https://discourse.julialang.org/t/instantiate-the-type-from-a-few-strings/10260 "2018-04-10T08:37:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 10, 2018, 8:37am UTC](https://discourse.julialang.org/t/instantiate-the-type-from-a-few-strings/10260/1 "2018-04-10T08:37:23Z")

</div>

I have a `Dict{String, <values>}` and I want to instantiate a type depending on the keys of that dict:

```julia
x = Dict("a" => 2, "b" => 0.0)
struct AnB
   a::Int 
   b::Float64
end
y = fun(x) # returns AnB(2, 0.0)

```

I’m going to have groups of dicts where each group has the exact same keys. I’ll make sure that there is always a type defined with the correct fields for each group of dicts. But **how do I write this `fun` function so it’ll accept any of the possible dicts?**

P.S.  
If it helps, I’ll add that the type of the keys of the dicts doesn’t _have_ to be `String`, but instead can be `Symbol`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 10, 2018, 9:00am UTC](https://discourse.julialang.org/t/instantiate-the-type-from-a-few-strings/10260/2 "2018-04-10T09:00:24Z")

</div>

```julia
julia> using Parameters

julia> @with_kw struct AnB
          a::Int
          b::Float64
       end
AnB

julia> x
Dict{String,Any} with 2 entries:
  "b" => 0.0
  "a" => 2

julia> AnB(; (Symbol(k) => v for (k, v) in x)...)
AnB
  a: Int64 2
  b: Float64 0.0

julia> x = Dict(:a => 2, :b => 0.0) # Symbols
Dict{Symbol,Any} with 2 entries:
  :a => 2
  :b => 0.0

julia> b = AnB(; x...)
AnB
  a: Int64 2
  b: Float64 0.0

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 10, 2018, 9:04am UTC](https://discourse.julialang.org/t/instantiate-the-type-from-a-few-strings/10260/3 "2018-04-10T09:04:06Z")

</div>

Thanks! I started tinkering with hashing the concatenation of the keys and using `Type{Val{<hash>}}`… Awesome that this is already solved.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 10, 2018, 9:40am UTC](https://discourse.julialang.org/t/instantiate-the-type-from-a-few-strings/10260/4 "2018-04-10T09:40:43Z")

</div>

Assuming that the type is somehow selected based on the keys, here is an example with symbols:

```julia
# define a method like below for each type
instantiate(dict::Dict{Symbol, <: Any}) =
    instantiate(Val(tuple(keys(dict)...)), values(dict)...)

struct AnB
   a::Int
   b::Float64
end

# you can automate this step with a macro on the struct
instantiate(::Val{(:a, :b)}, a, b) = AnB(a, b)

instantiate(Dict(:a => 2, :b => 0.0)) # AnB(2, 0.0)

```

BTW, this problem may be a very good match for named tuples.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 10, 2018, 10:24am UTC](https://discourse.julialang.org/t/instantiate-the-type-from-a-few-strings/10260/5 "2018-04-10T10:24:02Z")

</div>

Nice!
