问题
Is it possible to use only one parameter to do the following
type mytype{S}
x::Vector{S}
y::Vector{S} OR y::S
end
the value y should be able to be a vector of type S or just a single element of type S.
The reason I want this is because really I have
y::Dict{Vector{S}, Vector{Int64}}
and when the keys are just 1 element in length this is ugly
y["key"] #want this
y[["key"]] #must use this
回答1:
I think you need triangular dispatch for this. What you want is
type mytype{S,T<:Union{S,Vector{S}}}
x::Vector{S}
y::T
end
This will come in v0.6, see https://github.com/JuliaLang/julia/pull/18457
来源:https://stackoverflow.com/questions/40297172/julia-possible-to-use-only-1-parameter-for-vectors-or-s