# How to dispatch on a type alias?

**URL:** <https://discourse.julialang.org/t/how-to-dispatch-on-a-type-alias/106476>\
**Category:** New to Julia\
**Created:** [November 20, 2023, 5:27pm UTC](https://discourse.julialang.org/t/how-to-dispatch-on-a-type-alias/106476 "2023-11-20T17:27:46Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 20, 2023, 7:57pm UTC](https://discourse.julialang.org/t/how-to-dispatch-on-a-type-alias/106476/12 "2023-11-20T19:57:31Z")

</div>

> [@mbauman](#):
>
> I think that’s just a rule of how dispatch with type parameters works — it requires matching against a fully-resolved type.

I’ve been thinking of it in my head as method `where` clauses requiring the parameters be specified because they may be matched across multiple arguments or be accessible values in the method body. Reminds me of this past thread, I’ll just share the last small example sans the thread’s context:

> [@Question on manual subsection: Extracting the type parameter from a super-type](https://discourse.julialang.org/t/question-on-manual-subsection-extracting-the-type-parameter-from-a-super-type/97137/4):
>
> Incidentally, this is where it is important to specify parameters belonging to the argument versus the method. A version of the correct code for the number of dimensions would look like
> 
> ```julia
> my_ndims(::Type{<:AbstractArray{T,N} where T}) where {N} = N
> 
> my_ndims(Union{Array{Int,1}, Array{Bool,1}}) # 1
> 
> ```
> 
> `Base.ndims` now behaves like this, but it used to error on unspecific `T` like this `Union` or `Array{T,1} where T` because its method had both parameters `{T,N}` (issue [#40682](https://github.com/JuliaLang/julia/pull/40682)).

As for this particular case:

```julia
# works for _(C), fails for _(C{Int, Float64})
function myfunction(::Type{ A{T1,B{T3}} where {T1,T3} })
  return 1
end

# names disambiguated to avoid multimethods
# works for _(C{Int, Float64}), fails for _(C)
function myfunction1(::Type{ A{T1,B{T3}} }) where {T1,T3}
  return 1
end

function myfunction2(::Type{ A{T1,B{T3}} } where {T1, T3})
  return 1
end

```

As said before, method parameters must be specified, so `myfunction1` failing makes sense. `myfunction2` fails because the parametric `Type` requires that the `T1` and `T3` be specified, in the same way that `D = Vector{Ref{T}} where T` only includes `Vector{Ref{Int}}` or `Vector{Ref{Float64}}`, but not `Vector{Ref}`.

In fact `myfunction1` and `myfunction2` would override each other with the same function name because the argument requires specified `T1` and `T3`, despite not being equivalent (`T1` and `T3` are not available in the method body nor can they matched across arguments).

---

_[View the full topic](https://discourse.julialang.org/t/how-to-dispatch-on-a-type-alias/106476)._
