# Problem with parametric method definition

**URL:** <https://discourse.julialang.org/t/problem-with-parametric-method-definition/28683>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 12, 2019, 8:50am UTC](https://discourse.julialang.org/t/problem-with-parametric-method-definition/28683 "2019-09-12T08:50:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pnalysz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pnalysz/32/7306_2.png) [@pnalysz](https://discourse.julialang.org/u/pnalysz)\
**Post date:** [September 12, 2019, 8:50am UTC](https://discourse.julialang.org/t/problem-with-parametric-method-definition/28683/1 "2019-09-12T08:50:18Z")

</div>

Hi,

How to adjust definition of `f` so it works for both `A`s and `B`s types in the following code:

```julia
struct A end
struct B{b} end

va = Vector{Union{A, Missing}}()
vb = Vector{Union{B, Missing}}()

f(v::Vector{Union{t, Missing}}, i::t) where t = nothing  

f(va, A())
f(vb, B{:b}())

```

Of course I would like to avoid `i::Any` in the method definition.

Thanks,  
Piotr

---

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [September 12, 2019, 9:21am UTC](https://discourse.julialang.org/t/problem-with-parametric-method-definition/28683/2 "2019-09-12T09:21:01Z")

</div>

I am not sure if this is what you want to do, but if you do

` vb = Vector{Union{B{:b}, Missing}}()`

your code would work. In this case `t` would have the same type both in the array and in the scalar.

---

<div class="post-metadata">

**Author:** ![pnalysz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pnalysz/32/7306_2.png) [@pnalysz](https://discourse.julialang.org/u/pnalysz)\
**Post date:** [September 12, 2019, 9:25am UTC](https://discourse.julialang.org/t/problem-with-parametric-method-definition/28683/3 "2019-09-12T09:25:54Z")

</div>

I need `vb` to contain all type of `B`s.  
P

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 12, 2019, 10:00am UTC](https://discourse.julialang.org/t/problem-with-parametric-method-definition/28683/4 "2019-09-12T10:00:07Z")

</div>

Since `B` (without the parameter) is not a concrete type, I would recommend something like

```julia
f(v::Vector{Union{<: B, Missing}}, i::B) = :testB
f(v::Vector{Union{A, Missing}}, i::A) = :testA

```

I don’t think you can combine these into a single method signature, but someone may suggest a solution. In any case, I think that using two signatures is more readable.

It is possible that you want `AbstractVector` instead of `Vector`, unless you have a very compelling reason.

Finally, it is not clear from your example if you want to do this, but conditioning _semantics_ on container types is not good practice IMO. Code should work the same for `Vector{Any}` and `Vector{T}`, if the elements are otherwise identical. Also, if `vb` contains various `B{T}`s, you may not get any performance gains from narrowing the element type since `B` is not a concrete type.

---

<div class="post-metadata">

**Author:** ![pnalysz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pnalysz/32/7306_2.png) [@pnalysz](https://discourse.julialang.org/u/pnalysz)\
**Post date:** [September 13, 2019, 7:33am UTC](https://discourse.julialang.org/t/problem-with-parametric-method-definition/28683/5 "2019-09-13T07:33:09Z")

</div>

Thanks a lot for explanations.  
Piotr
