# Dispatch on NamedTuple

**URL:** <https://discourse.julialang.org/t/dispatch-on-namedtuple/37154>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 7, 2020, 9:13am UTC](https://discourse.julialang.org/t/dispatch-on-namedtuple/37154 "2020-04-07T09:13:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![corbat](https://avatars.discourse-cdn.com/v4/letter/c/e0b2c6/32.png) [@corbat](https://discourse.julialang.org/u/corbat)\
**Post date:** [April 7, 2020, 9:13am UTC](https://discourse.julialang.org/t/dispatch-on-namedtuple/37154/1 "2020-04-07T09:13:30Z")

</div>

Hi,

I was wondering if there is a way to dispatch on NamedTuple function arguments based on the types used within the NamedTuple without knowing the number of elements inside but with all elements having the same type.

So I would like to do something like this below:

```
function foo(nt::NamedTuple{Symbol, MyType})
	println("The argument is a NamedTuple containing MyType elements")
end
	
function foo(nt::NamedTuple{Symbol, AnotherMyType})
	println("The argument is a NamedTuple containing AnotherMyType elements")
end

```

So that

```
nt = (a = MyType(5), b = MyType(6), c = MyType(55))
nt2 = (x = AnotherMyType(2), y = AnotherMyType(3))

foo(nt)
The argument is a NamedTuple containing MyType elements

foot(nt2)
The argument is a NamedTuple containing AnotherMyType elements

```

Is there a better way to achieve this than doing something like this:

```
function foo(nt::NamedTuple)

	if isa(foo[1], MyType) 
		println("The argument is a NamedTuple containing MyType elements")
		
	elseif isa(foo[1], AnotherMyType) 
		println("The argument is a NamedTuple containing AnotherMyType elements")
	end
	
end

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [April 7, 2020, 9:33am UTC](https://discourse.julialang.org/t/dispatch-on-namedtuple/37154/2 "2020-04-07T09:33:14Z")

</div>

Sure, the type of the ordinary tuple is part of the type of the named tuple:

```julia
julia> typeof((a=1, b=2))
NamedTuple{(:a, :b),Tuple{Int64,Int64}}

julia> ans <: NamedTuple{<:Any, <:Tuple{Vararg{Int}}}
true

julia> (a=1, b=2, c=3) isa NamedTuple{<:Any, <:Tuple{Vararg{Int}}}
true

```

---

<div class="post-metadata">

**Author:** ![corbat](https://avatars.discourse-cdn.com/v4/letter/c/e0b2c6/32.png) [@corbat](https://discourse.julialang.org/u/corbat)\
**Post date:** [April 7, 2020, 10:37am UTC](https://discourse.julialang.org/t/dispatch-on-namedtuple/37154/3 "2020-04-07T10:37:42Z")

</div>

Ah, thanks a lot! The `Vararg` is what I was missing.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [April 8, 2020, 1:06pm UTC](https://discourse.julialang.org/t/dispatch-on-namedtuple/37154/4 "2020-04-08T13:06:50Z")

</div>

Using a `NTuple` instead of `Vararg`, you can also get the number of elements if it matters to you:

```julia
julia> function foo(nt::NamedTuple{<:Any, NTuple{N, T}}) where {N, T}
           "Argument is a NamedTuple containing $N elements of type $T"
       end
foo (generic function with 1 method)

julia> foo((x=1, y=1))
"Argument is a NamedTuple containing 2 elements of type Int64"

```
