# A question on dispatch on type parameter being UnionAll

**URL:** <https://discourse.julialang.org/t/a-question-on-dispatch-on-type-parameter-being-unionall/132070>\
**Category:** General Usage\
**Tags:** dispatch\
**Created:** [September 3, 2025, 10:41am UTC](https://discourse.julialang.org/t/a-question-on-dispatch-on-type-parameter-being-unionall/132070 "2025-09-03T10:41:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [September 3, 2025, 10:41am UTC](https://discourse.julialang.org/t/a-question-on-dispatch-on-type-parameter-being-unionall/132070/1 "2025-09-03T10:41:31Z")

</div>

I’m a bit curious about the following problem, it’s not a big deal and has an easy solution, but anyway

```julia-auto
struct A{T,S}
    t::T
    s::S
end

struct B{V<:A}
    v::Vector{V}
end

b1 = B([A(1,2), A(3,4.0)]) # element type is UnionAll
b2 = B([A(1,2), A(3,4)]) # element type is concrete

function f(b::B{T}) where {T}
    T isa UnionAll ? "UnionAll" : Symbol("not UnionAll")
end

```

The function `f` checks if the element type of `b.v` is a `UnionAll`, and takes different code paths.  
Is there a way to handle this by dispatch instead of the `isa` test? I.e. have two methods for `f` with different signatures?  
There’s of course this kludge:

```julia-auto
g(b::B{T}) where {T} = g(b, typeof(T))
g(b::B, ::Type{UnionAll}) = "UnionAll"
g(b::B, ::Any) = Symbol("not UnionAll")

```

Both `f` and `g` are type stable, so things are handled at compile time, but in a bit non-julian fashion.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [September 3, 2025, 10:51am UTC](https://discourse.julialang.org/t/a-question-on-dispatch-on-type-parameter-being-unionall/132070/2 "2025-09-03T10:51:48Z")

</div>

No you can’t dispatch on the type of a type in the same position as dispatching on the type itself.

Your solution with a second function is probably the neatest way if you really want to use dispatch, though if I were you I’d just branch like you did originally.

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [September 3, 2025, 12:02pm UTC](https://discourse.julialang.org/t/a-question-on-dispatch-on-type-parameter-being-unionall/132070/3 "2025-09-03T12:02:38Z")

</div>

Do you mean this?

```julia
v::Vector{Vector{Int64}} = [[1]]

function f_okay(v::Vector{T}) where T
    T
end
f_okay(v)

function f_no_support(v::Vector{T}) where T::Vector{<:Integer}
    T
end;
f_no_support(v)

```

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [September 3, 2025, 1:26pm UTC](https://discourse.julialang.org/t/a-question-on-dispatch-on-type-parameter-being-unionall/132070/4 "2025-09-03T13:26:52Z")

</div>

No, if you look closely at the example:

```julia-auto
julia> a = [A(1,2), A(3,4.0)]
2-element Vector{A{Int64}}:
 A{Int64, Int64}(1, 2)
 A{Int64, Float64}(3, 4.0)

```

The element type of `a` is `A{Int64}` which is an “incomplete” type, it’s really `A{Int64, S} where S`, with a free type variable `S`, i.e. a `UnionAll` type:

```julia-auto
julia> typeof(eltype(a))
UnionAll

julia> dump(A{Int})
UnionAll
  var: TypeVar
    name: Symbol S
    lb: Union{}
    ub: abstract type Any
  body: struct A{Int64, S} <: Any
    t::Int64
    s::S

julia> dump(A{Int,S} where S)
UnionAll
  var: TypeVar
    name: Symbol S
    lb: Union{}
    ub: abstract type Any
  body: struct A{Int64, S} <: Any
    t::Int64
    s::S

```

And I wanted to know if this can be picked up by dispatch, something like

```julia-auto
function f(b::B{T}) where {T isa UnionAll} # unsupported
    ...
end

```

to distinguish it from when the element type (`T`) is e.g. `A{Int, Int}` or `A{Int, Float64}`, which is a “complete” `DataType` not a `UnionAll`.
