Vector{AbstractString} function parameter won't accept Vector{String} input in julia

后端 未结 1 2005
一向
一向 2020-12-06 18:15

The following code in Julia:

function foo(a::Vector{AbstractString})  
end
foo([\"a\"])

gives the following error:

ERROR:         


        
相关标签:
1条回答
  • 2020-12-06 18:43

    You need to write the function signature like this:

    function foo{S<:AbstractString}(a::Vector{S})
        # do stuff
    end
    

    On Julia 0.6 and newer, it's also possible to write instead

    function foo(a::Vector{<:AbstractString})
        # do stuff
    end
    

    This is a consequence of parametric type invariance in Julia. See the chapter on types in the manual for more details.

    0 讨论(0)
提交回复
热议问题