简体   繁体   中英

How to use an union of array and parametric NTuple as function argument type in Julia?

I am trying to write a function that takes an argument that can be a tuple or an array. This works for example:

julia> temp(x::Union{Vector{Int64},NTuple{4,Int64}}) = sum(x)
temp (generic function with 1 method)

julia> temp((3,1,5,4))
13

julia> temp([3,1,5,4])
13

On the other hand, when I try to use a tuple of an unspecified length, it fails for the array:

julia> temp(x::Union{Vector{Int64},NTuple{N,Int64}}) where N = sum(x)
temp (generic function with 1 method)

julia> temp([3,1,5,4])
ERROR: MethodError: no method matching temp(::Array{Int64,1})
Closest candidates are:
  temp(::Union{Array{Int64,1}, Tuple{Vararg{Int64,N}}}) where N at REPL[1]:1

julia> temp((3,1,5,4))
13

Is this not the way of doing things? I realise that I can solve this using multiple dispatch:

julia> temp(x::Vector{Int64}) = sum(x)
temp (generic function with 1 method)

julia> temp(x::NTuple{N,Int64}) where N = sum(x)
temp (generic function with 2 methods)

julia> temp((3,1,5,4))
13

julia> temp([3,1,5,4])
13

but I am trying to understand how Union works in julia, and wondering if there is a way to achieve this using it.

The behavior differs between Julia 0.6.3 and Julia 0.7-alpha. What we have in Julia 0.7-alpha is more consistent as location of where clause does not matter in this case.

Case of Julia 0.6.3

You have two ways to fix the problem by moving where clause inside function definition:

julia> temp1(x::Union{Vector{Int64},NTuple{N,Int64}} where N) = sum(x)
temp1 (generic function with 1 method)

julia> temp1([3,1,5,4])
13

julia> temp1((3,1,5,4))
13

julia> temp2(x::Union{Vector{Int64},NTuple{N,Int64} where N}) = sum(x)
temp2 (generic function with 1 method)

julia> temp2([3,1,5,4])
13

julia> temp2((3,1,5,4))
13

also you can avoid the need to specify where N by using Vararg like this:

julia> temp3(x::Union{Vector{Int64}, Tuple{Vararg{Int64}}}) = sum(x)
temp3 (generic function with 1 method)

julia> temp3((3,1,5,4))
13

julia> temp3([3,1,5,4])
13

Case of Julia 0.7-alpha

Your function will just work:

julia> temp(x::Union{Vector{Int64},NTuple{N,Int64}}) where N = sum(x)
temp (generic function with 1 method)

julia> temp([3,1,5,4])
13

julia> temp((3,1,5,4))
13

also temp1 , temp2 and temp3 will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM