Julia parametric function for array array with multiple sending

I am trying to write two functions with the same name that can use Julia's multiple dispatch system and include arrays and arrays of arrays.

Suppose these three input variables

# typeof(a) => Array{Array{T,N},1}
a=Array[[1,2,3],[4,5,6]]

# typeof(b) => Array{Int64,1}
b=[1,2,3]

# typeof(c) => Array{Array{Float64,1},1}
c=[rand(10) for i in 1:2]

And I write two functions like

function test(x::Array{Array})
    println("received an array of array => $(typeof(x))")
end

function test{T<:Number}(x::Array{T,1})
    println("received a vector. converting.....")
    nx = Array[x]
    test(nx)
end

This approach works for aand b, but not c.

test(a)
# received an array of array => Array{Array{T,N},1}

test(b)
# received a vector. converting.....
# received an array of array => Array{Array{T,N},1}

test(c)
# LoadError: MethodError: `test` has no method matching test(::Array{Array{Float64,1},1})
# while loading In[37], in expression starting on line 1

Now, if I change the definition of a function to x::Array{Array{Float64,1},1}, this will work. But my question is not what Array{Array}should any array arrays accept?

How to work with an internal type parameter in an array of arrays?

+4
source share
1 answer

is not Array {Array} ?

... , .

, , Julias .

, Array , Array{T} T<:Array Array{Array}:

function test{T<:Array}(x::Array{T})
    println("received an array of array => $(typeof(x))")
end
+4

Source: https://habr.com/ru/post/1623500/


All Articles