Understanding the use of Union with parametric types

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:

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> 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

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.

1 Like

Interesting. Because the foo2function 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!

Related: Weird bug(?) with dispatch(?) in 0.4 · Issue #13156 · JuliaLang/julia · GitHub