# Why do functions whose arguments are named tuples fail when one element of the named tuple has type Any?

**URL:** <https://discourse.julialang.org/t/why-do-functions-whose-arguments-are-named-tuples-fail-when-one-element-of-the-named-tuple-has-type-any/90172>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 12, 2022, 9:47pm UTC](https://discourse.julialang.org/t/why-do-functions-whose-arguments-are-named-tuples-fail-when-one-element-of-the-named-tuple-has-type-any/90172 "2022-11-12T21:47:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [November 12, 2022, 9:47pm UTC](https://discourse.julialang.org/t/why-do-functions-whose-arguments-are-named-tuples-fail-when-one-element-of-the-named-tuple-has-type-any/90172/1 "2022-11-12T21:47:21Z")

</div>

I defined a function that prints the elements of a named tuple. I do not  
know the type of the second element of the named tuple, so I set it to Any.

```julia
function foo(nt::NamedTuple{(:A, :V), Tuple{String, Any}})
    println(A * " " * string(V))
end

```

Why does this function generate an error?

```julia
julia> foo((A = "Hello", V = 1))
ERROR: MethodError: no method matching foo(::NamedTuple{(:A, :V), Tuple{String, Int64}})
Closest candidates are:
  foo(::NamedTuple{(:A, :V), Tuple{String, Any}}) at REPL[1]:1
Stacktrace:
 [1] top-level scope

```

How can I fix it so that it works for V of any type?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 12, 2022, 10:09pm UTC](https://discourse.julialang.org/t/why-do-functions-whose-arguments-are-named-tuples-fail-when-one-element-of-the-named-tuple-has-type-any/90172/2 "2022-11-12T22:09:55Z")

</div>

To fix the dispatch you can do this:

```julia
julia> function foo(nt::NamedTuple{(:A, :V), Tuple{A,V}}) where {A,V}
       ...    
       end

```

But with your function body you will still get an error, because, in your original definition `A` and `V` are not defined. With my correction `A` and `V` are types which will error now in your `println` call.

So I guess what you want to do with this examples:

```julia
function foo(nt::NamedTuple{(:A, :V), Tuple{A,V}}) where {A,V}
    println( string(nt[:A]) * " is type " * string(A) * " and " * string(nt[:V]) * " is type " * string(V) )
end

```

```julia
julia> foo((A = "Hello", V = 1))
Hello is type String and 1 is type Int64

julia> foo((A = "Hello", V = 1.0))
Hello is type String and 1.0 is type Float64

julia> foo((A = "Hello", V = "world"))
Hello is type String and world is type String

```

---

<div class="post-metadata">

**Author:** ![brendaisy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brendaisy/32/32810_2.png) [@brendaisy](https://discourse.julialang.org/u/brendaisy)\
**Post date:** [November 12, 2022, 10:34pm UTC](https://discourse.julialang.org/t/why-do-functions-whose-arguments-are-named-tuples-fail-when-one-element-of-the-named-tuple-has-type-any/90172/3 "2022-11-12T22:34:26Z")

</div>

There is no matching method because type parameters are invariant in Julia. So, just because `Int64 <: Any` does not mean

```julia
julia> NamedTuple{(:A, :V), Tuple{String, Int64}} <: NamedTuple{(:A, :V), Tuple{String, Any}}
false

```

To print the values of a NamedTuple as a single string, a simple option is

```julia
julia> foo(x::NamedTuple) = join(values(x), " ")

julia> foo((A = "Hello", V = 1))
"Hello 1"

julia> foo((a=Dict(:x=>1), b="bar"))
"Dict(:x => 1) bar"

```

---

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [November 13, 2022, 7:39am UTC](https://discourse.julialang.org/t/why-do-functions-whose-arguments-are-named-tuples-fail-when-one-element-of-the-named-tuple-has-type-any/90172/4 "2022-11-13T07:39:00Z")

</div>

Thanks. Now I see that I needed to do `nt[:A]` instead of just A in the call to `println`. And similarly for V.
