0

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
mv3
  • 469
  • 5
  • 16
  • @Chris Rackauckas's answer is correct, I believe, but I wonder if this is what is what you really meant by the question. With triangular dispatch you get a `y` which is *either* `Dict{Vector{S}, Vector{Int}}` *or* `Dict{S, Vector{Int}}`. You do not get `y` that can take both `S` and `Vector{S}` as keys. – DNF Oct 28 '16 at 07:31
  • 1
    You can consider keeping `y` strictly of type `Vector{S}`, but then manipulate the getters and setters to accept either `Vector{S}` or `S` as keys. – DNF Oct 28 '16 at 07:46

1 Answers1

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

Chris Rackauckas
  • 18,645
  • 3
  • 50
  • 81
  • I just remembered about Union after I posted this. Is it possible to just do: x::T, y::T since Vector{S} <: T and keep only the T parameter? – mv3 Oct 28 '16 at 02:51
  • Well i just tried it and my comment does not work. I guess once T is selected it is fixed and can't be used for both. – mv3 Oct 28 '16 at 03:03
  • Yes, what you're looking for is triangular dispatch. – Chris Rackauckas Oct 28 '16 at 03:18