1

I am clearly missing something fundamental here. How can I change the second method of my function to accept c? Additionally, I would really prefer to use AbstractArray instead of AbstractVector as the type so as not to restrict the dimension either, but I wanted to cut down on potential sources of error.

function f(x::AbstractVector{Int})
    println(x)
end # f (generic function with 1 method)

function f(x::AbstractVector{AbstractVector{Int}})
    for i in x
        println(i)
    end
end # f (generic function with 2 methods)

a=[1,2,3] # 3-element Array{Int64,1}:
b=[4,5,6] # 3-element Array{Int64,1}:
c=[a,b] # 2-element Array{Array{Int64,1},1}:

typeof(a) <: AbstractVector{Int} # true
typeof(c) <: AbstractVector{AbstractVector{Int}} # false

f(a) # [1, 2, 3]
f(c) # ERROR: MethodError: no method matching f(::Array{Array{Int64,1},1})
1

1 Answer 1

3

Quoting the manual:

Concrete Point types with different values of T are never subtypes of each other:

julia> struct Point{T} end

julia> Point{Float64} <: Point{Int64}
false

julia> Point{Float64} <: Point{Real}
false

This last point is very important: even though Float64 <: Real we DO NOT have Point{Float64} <: Point{Real}.

Translated to your example, as you noted,

julia> Vector{Int} <: AbstractVector{Int}
true

julia> Vector{Vector{Int}} <: AbstractVector{AbstractVector{Int}}
false

What you need here is

julia> Vector{Vector{Int}} <: AbstractVector{T} where T <: AbstractVector{Int}
true

or the convenient shorthand

julia> Vector{Vector{Int}} <: AbstractVector{<:AbstractVector{Int}}
true
Sign up to request clarification or add additional context in comments.

2 Comments

1. Why would Float64 be a subtype of Int64? I don't follow that example or how it relates to my first type test. 2. Int is a supertype of Int64 like AbstractVector is a supertype of Vector. I don't understand why the rules change arbitrarily at the third level i.e. why don't I need <:AbstractVector{<:Int}}? 3. Are there any additional code changes that I need to use AbstractArray in place of AbstractVector?
1. The example is perhaps taken out of context a bit since I only quoted part of the section. I suggest you read the full section to get the context. 2. The rules don't change; Int === Int64 (on a 64-bit system) so you can not compare the abstract type AbstractVector with the concrete type Int. You can, however, try with the abstract integer supertype Integer and see that the rules are consistent with what you see for AbstractVector. 3. I am not sure what you are asking; If you want to handle more than just vectors you can use AbstractArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.