# Correct type in function for Dict of NTuple

**URL:** <https://discourse.julialang.org/t/correct-type-in-function-for-dict-of-ntuple/83474>\
**Category:** General Usage\
**Tags:** question, type, dictionary, ntuple\
**Created:** [June 28, 2022, 5:24pm UTC](https://discourse.julialang.org/t/correct-type-in-function-for-dict-of-ntuple/83474 "2022-06-28T17:24:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christmas\_jones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christmas_jones/32/29540_2.png) [@christmas\_jones](https://discourse.julialang.org/u/christmas_jones)\
**Post date:** [June 28, 2022, 5:24pm UTC](https://discourse.julialang.org/t/correct-type-in-function-for-dict-of-ntuple/83474/1 "2022-06-28T17:24:22Z")

</div>

I need a function which takes a Dictionary, where keys are Integer Tuples, and values are Float64 types.

I do not know in advance the length of the Tuples, so I would be tempted to use the following type in the function definition (this function is just an example):

```julia
function tuple_dict_fun(d:: Dict{Tuple{Vararg{Int}}, Float64})
    return d
end

```

At runtime, I will create dictionaries in the following manner: `dicto = Dict((1,2)=>2.0)`, or `dicto = Dict((1,2,3)=>2.0)` (i.e. they keys are always tuples of int, but the length of the key is not fixed or known ahead of time).

However, calling the above function with dicto will yield an error such as

```julia
tuple_dict_fun(dicto)

MethodError: no method matching tuple_dict_fun(::Dict{Tuple{Int64, Int64, Int64}, Float64})
Closest candidates are:
  tuple_dict_fun(::Dict{Tuple{Vararg{Int64}}, Float64}) at In[1]:1

```

The reason is because the defined `dicto` has keys of a specific type `NTuple{3, Int}`, rather than the Vararg type. How should I specify the dictionaries type in the function in such a situation? Trivially I can just use `d:: Dict` and that works, but it would be nice to have something closer to what is actually being passed.

Similarly, when I construct the dictionaries, I could also set it to match the type in the above function definition, but that seems a little silly, since each given realization does have fixed length keys that are known at runtime.

Again, I stress, the keys are always tuples of int, but I do not know ahead of time the length of the tuples, so can not put that directly in the type definition. In a given dictionary which I want to pass to the function, all keys are the same length however (e.g. all length 3 or all length 2).

Thanks!

---

<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:** [June 28, 2022, 5:44pm UTC](https://discourse.julialang.org/t/correct-type-in-function-for-dict-of-ntuple/83474/2 "2022-06-28T17:44:53Z")

</div>

more generally, with various length tuples of elements of type T

```julia
dict3 = Dict( (1,2) => 2.0, (1,2,3) => 3.0 )

accept(x) = false
accept(d::Dict{T, Float64}) where {T<:NTuple} = true

accept(dict3) == true

```

if you prefer the more specific

```julia
 julia> function fn(d:: Dict{NTuple{N,Int}, Float64}) where N
     return d
 end

julia> dict1 = Dict((1,2)=>2.0);
julia> dict2 = Dict((1,2,3)=>2.0);

julia> fn(dict1)
Dict{Tuple{Int64, Int64}, Float64} with 1 entry:
  (1, 2) => 2.0

julia> fn(dict2)
Dict{Tuple{Int64, Int64, Int64}, Float64} with 1 entry:
  (1, 2, 3) => 2.0

```

---

<div class="post-metadata">

**Author:** ![christmas\_jones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christmas_jones/32/29540_2.png) [@christmas\_jones](https://discourse.julialang.org/u/christmas_jones)\
**Post date:** [June 28, 2022, 6:12pm UTC](https://discourse.julialang.org/t/correct-type-in-function-for-dict-of-ntuple/83474/3 "2022-06-28T18:12:55Z")

</div>

Thanks so much, this was _exactly_ what I needed!!
