Hi,
I’m struggling with type signatures. The following code only works if all the types I pass are of the exact same type.
abstract type S end
struct S1 <: S
value::Int64
end
struct S2 <: S
value::Float64
end
struct Container
elements::Dict{S, Vector{S}}
Container() = new(Dict{S, Vector{S}}())
end
function Container(source::Dict{T, Vector{T}} where {T <: S})
return Container()
end
So,
Container(Dict(S1(1) => [S1(2), S1(3)]))
works, but the following results in the accompanying error.
Container(Dict(S1(1) => [S1(2), S2(3)]))
ERROR: MethodError: no method matching Container(::Dict{S1,Array{Any,1}})
Closest candidates are:
Container() at none:3
Container(::Dict{T,Array{T,1}} where T<:S) at none:1
Stacktrace:
[1] top-level scope at none:1
Digging a bit deeper I found that I can create the following:
d = Dict{S, Vector{S}}()
to which I can add a mixture of elements of types S1 and S2.
I want to avoid having to write the explicit types all the times though so I would like to solve it in the function signature. Is this possible?
When creating a collection with several elements of different types, the type of the collection is set to Any. I wonder whether it would be an idea to use a common type ancestor if possible. That would probably mean an alteration to the language though I guess.
Thanks in advance,
Stef