鉴于:
julia> SubString <: String
false
如何编写同时接受子字符串和字符串的函数?
julia> function myfunction(ss::String)
@show ss, typeof(ss)
end
myfunction (generic function with 1 method)
julia> myfunction("Hello World")
(ss, typeof(ss)) = ("Hello World", String)
("Hello World", String)
julia> s = split("Hello World")
2-element Array{SubString{String},1}:
"Hello"
"World"
julia> foreach(x -> myfunction(x), s)
ERROR: MethodError: no method matching myfunction(::SubString{String})
Closest candidates are:
myfunction(::String) at REPL[11]:2