# Understanding the use of Union with parametric types

**URL:** <https://discourse.julialang.org/t/understanding-the-use-of-union-with-parametric-types/337>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [November 16, 2016, 12:55am UTC](https://discourse.julialang.org/t/understanding-the-use-of-union-with-parametric-types/337 "2016-11-16T00:55:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [November 16, 2016, 12:55am UTC](https://discourse.julialang.org/t/understanding-the-use-of-union-with-parametric-types/337/1 "2016-11-16T00:55:11Z")

</div>

Hi I’m trying to understand a `MethodError` I encountered after setting an argument type of a method to be the union of two types, one of which is a parametric type. The following mwe reproduces the issue.

Consider the following three alternate definitions of the `foo` function:

```julia
function foo1{T1<:AbstractFloat,T2<:Union{Future,AbstractFloat}}(
              a::Int64,
              b::Array{T1},
              jumble::Array{T2}=ones(length(b)))

  return b[a]*fetch(jumble[a])
end

function foo2{T<:AbstractFloat}(
              a::Int64,
              b::Array{T},
              jumble::Union{Array{Future},Array{T}}=ones(length(b)))

  return b[a]*fetch(jumble[a])
end

function foo3{T<:AbstractFloat}(
              a::Int64,
              b::Array{Float64},
              jumble::Union{Array{Future},Array{T}}=ones(length(b)))

  return b[a]*fetch(jumble[a])
end

```

Note that the only difference between `foo2` and `foo3` is in the array element types allowed in the array `b`. Now I try to run these three functions with different input arguments:

```julia
julia> a = 2;

julia> b = rand(4);

julia> c = rand(4);

julia> cRefs = Array{Future}(4);

julia> for i = 1:4
         cRefs[i] = @spawn c[i];
       end

julia> foo1(a,b)
0.7997957884868263

julia> foo2(a,b)
0.7997957884868263

julia> foo3(a,b)
0.7997957884868263

julia> foo1(a,b,c)
0.6842898455843193

julia> foo2(a,b,c)
0.6842898455843193

julia> foo3(a,b,c)
0.6842898455843193

julia> foo1(a,b,cRefs)
0.6842898455843193

julia> foo2(a,b,cRefs)
0.6842898455843193

julia> foo3(a,b,cRefs)
ERROR: MethodError: no method matching foo3(::Int64, ::Array{Float64,1}, ::Array{Future,1})
Closest candidates are:
  foo3{T<:AbstractFloat}(::Int64, ::Array{Float64,N}, ::Union{Array{Future,N},Array{T<:AbstractFloat,N}}) at /home/patrick/test.jl:24
  foo3(::Int64, ::Array{Float64,N}) at /home/patrick/test.jl:24

```

When calling any of the functions with two arguments or with the third argument being an array of floating point numbers everything works as expected but when the third argument has type `Array{Future}`, `foo1` and `foo2` behave as expected but `foo3` throws a `MethodError`. Using the `foo1` syntax seems to be the best way to write this code even if I didn’t run into the error with `foo3` but I’m curious about what’s going on. I would have thought Julia would be able to deal with the case that generated the error shown above. Is this a bug or am I missing something?

Thanks, Patrick

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 16, 2016, 1:22am UTC](https://discourse.julialang.org/t/understanding-the-use-of-union-with-parametric-types/337/2 "2016-11-16T01:22:13Z")

</div>

For a method to be callable, you have to fix all the type parameters. If only the none parameter part of the union matches, it cannot fix the type parameter and so the match fails.

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [November 16, 2016, 1:57am UTC](https://discourse.julialang.org/t/understanding-the-use-of-union-with-parametric-types/337/3 "2016-11-16T01:57:36Z")

</div>

Interesting. Because the `foo2`function worked I was wondering if this had something to do with Julia not being able to fix the type parameter. Thanks for the insight Yichao!

---

<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:** [November 16, 2016, 9:11am UTC](https://discourse.julialang.org/t/understanding-the-use-of-union-with-parametric-types/337/4 "2016-11-16T09:11:47Z")

</div>

Related: [Weird bug(?) with dispatch(?) in 0.4 · Issue #13156 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/13156)
