# Mutiple dispatch on Tuple or "scalar"?

**URL:** <https://discourse.julialang.org/t/mutiple-dispatch-on-tuple-or-scalar/100742>\
**Category:** General Usage\
**Created:** [June 23, 2023, 2:13pm UTC](https://discourse.julialang.org/t/mutiple-dispatch-on-tuple-or-scalar/100742 "2023-06-23T14:13:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [June 23, 2023, 2:13pm UTC](https://discourse.julialang.org/t/mutiple-dispatch-on-tuple-or-scalar/100742/1 "2023-06-23T14:13:12Z")

</div>

I want two versions of a function depending on whether the argument is a `Tuple{T,T}` or just `T`. I thought the more specific one wins, but in the code at the end of this post, the less specific `x::T` always wins over `x::Tuple{T,T}`.

My goal right now is

```julia
f(x = (3,5)) # -> calls f(x::Tuple{Int,Int})
f(x = 3) # -> calls f(x::Int)

```

How does one do this?

A more general question is, how do you have two versions, one expecting a “scalar” and the other expecting a “collection” (array, range, tuple)?

```julia
function f(; x::Tuple{T,T}) where {T}
  print("tuple ")
  print(typeof(x), " ")
  @show x
end
function f(; x::T) where {T}
  print("single ")
  print(typeof(x), " ")
  @show x
end

f(x = (3,4)) # -> single Tuple{Int64, Int64} x = (3, 4)
f(x = (3,"5")) # -> single Tuple{Int64, String} x = (3, "5")
f(x = 9) # -> single Int64 x = 9

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 23, 2023, 2:16pm UTC](https://discourse.julialang.org/t/mutiple-dispatch-on-tuple-or-scalar/100742/2 "2023-06-23T14:16:57Z")

</div>

see [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/#Note-on-Optional-and-keyword-Arguments). Specifically

> Methods are dispatched based only on positional arguments, with keyword arguments processed after the matching method is identified.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [June 23, 2023, 2:18pm UTC](https://discourse.julialang.org/t/mutiple-dispatch-on-tuple-or-scalar/100742/3 "2023-06-23T14:18:40Z")

</div>

… and see a related discussion at [How can kwargs be used in multiple dispatch](https://discourse.julialang.org/t/how-can-kwargs-be-used-in-multiple-dispatch/86790) 🙂

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 23, 2023, 2:21pm UTC](https://discourse.julialang.org/t/mutiple-dispatch-on-tuple-or-scalar/100742/4 "2023-06-23T14:21:21Z")

</div>

Although of course you can use dispatch internally to do that:

```julia
julia> _f(x::T) where {T} = 1
_f (generic function with 1 method)

julia> _f(x::Tuple{T,T}) where {T} = 2
_f (generic function with 2 methods)

julia> f(;x) = _f(x)
f (generic function with 1 method)

julia> f(x=1)
1

julia> f(x=(2,3))
2

```

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [June 23, 2023, 2:58pm UTC](https://discourse.julialang.org/t/mutiple-dispatch-on-tuple-or-scalar/100742/5 "2023-06-23T14:58:17Z")

</div>

Thank you all for your help.

> [@Oscar\_Smith](#):
>
> Methods are dispatched based only on positional arguments

Ah! That’s what I was missing!

```julia
f(;x) = _f(x)

```

I see!

Out of curiosity . . . why not? Why isn’t dispatch on keyword arguments allowed? (I googled it but wasn’t able to locate a discussion for the reason.)
