# Tricky parametric type dispatch

**URL:** https://discourse.julialang.org/t/tricky-parametric-type-dispatch/23064
**Category:** General Usage
**Tags:** parametric-types
**Created:** [April 12, 2019, 5:33am UTC](https://discourse.julialang.org/t/tricky-parametric-type-dispatch/23064 "2019-04-12T05:33:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 12, 2019, 5:33am UTC](https://discourse.julialang.org/t/tricky-parametric-type-dispatch/23064/1 "2019-04-12T05:33:10Z")

</div>

I would like to dispatch a method like this, where `T` gets the type `<: AbstractParticles` and the type parameters end up in `F` and `NT`.

```julia
function Base.union(p1::T{F,NT},p2::S{F,NS}) where T <: AbstractParticles where S <: AbstractParticles{F,NS} where F where NT where NS
    T{F,NT+NS}([p1.particles; p2.particles])
end

```

This does not work and an error

```julia
TypeError: in Type{...} expression, expected UnionAll, got TypeVar

```

occurs. The following definition is accepted, but it makes `T` include the type parameters `T{F,NT}` which I do not want.

```julia
function Base.union(p1::T,p2::S) where T <: AbstractParticles{F,NT} where S <: AbstractParticles{F,NS} where F where NT where NS
    @show T
end
julia> union(p,p)
#T = Particles{Float64,1000}

```

Is there another way to express the former, or to strip `Particles{Float64,1000}` from the type parameters without using `eval`?

Edit:  
The following works, but feels like a hack

```julia
function Base.union(p1::T,p2::S) where T <: AbstractParticles{F,NT} where S <: AbstractParticles{F,NS} where F where NT where NS
    T.name.wrapper{F,NT+NS}([p1.particles; p2.particles])
end

```

It does not feel like I’m supposed to use the `T.name.wrapper`

Edit 2:  
I can also put the function definition in a loop over the different possible `T` and `@eval` the function into existence.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [April 12, 2019, 6:18am UTC](https://discourse.julialang.org/t/tricky-parametric-type-dispatch/23064/2 "2019-04-12T06:18:16Z")

</div>

No, you cannot do this in general. This is why `similar` exists for arrays and you should probably use a similar approach. X-ref:

> [@Stripping parameter from parametric types](https://discourse.julialang.org/t/stripping-parameter-from-parametric-types/8293/10):
>
> No, you can’t. The approved solution is wrong – there’s no relation between the ordering of type parameters of a subtype (in this case, SomeArrayType) and its supertype (AbstractArray). Tamas\_Papp’s solution is correct. In generic code, this is usually done by calling the similar function. However, we can also generalize that solution to abstract types as follows (by adding in \<: so that it can select subtypes): stripN(::Type{\<:AbstractArray{T, N}}) where {T, N} = AbstractArray{T, M} where M

---

<div class="post-metadata">

### Author: ![anon67531922](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anon67531922](https://discourse.julialang.org/u/anon67531922)
#### Post date: [April 12, 2019, 7:06am UTC](https://discourse.julialang.org/t/tricky-parametric-type-dispatch/23064/3 "2019-04-12T07:06:11Z")

</div>

Hi @baggepinnen 👋

> [@baggepinnen](#):
>
> The following definition is accepted, but it makes `T` include the type parameters `T{F,NT}` which I do not want.

Out of curiosity, why don’t you want this? After all, `T` does include parameters `F` and `NT` according to your first attempt above.

The accepted definition looks fine to me so I am curious why you don’t like it.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 12, 2019, 12:00pm UTC](https://discourse.julialang.org/t/tricky-parametric-type-dispatch/23064/4 "2019-04-12T12:00:53Z")

</div>

I didn’t want the type parameters included as I would like to modify them, in my case call `T{F,NT+NS}`.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 12, 2019, 12:02pm UTC](https://discourse.julialang.org/t/tricky-parametric-type-dispatch/23064/5 "2019-04-12T12:02:04Z")

</div>

Thanks for the link, which led me to this section of the manual  
[https://docs.julialang.org/en/latest/manual/methods/#Building-a-similar-type-with-a-different-type-parameter-1](https://docs.julialang.org/en/latest/manual/methods/#Building-a-similar-type-with-a-different-type-parameter-1)  
which is exactly what I would like to do
