I’m having trouble understand parametric types and mixing parameters in collections. For example,
struct Foo{T, S}
a::T
b::S
end
If I have this definition I can do the following:
a = Foo(1, 2)
b = Foo(1., 2)
c = Foo(1., 2.)
v = Foo[]
push!(v, a)
push!(v, b)
push!(v, c)
I know v
is going to be filled with items of type Foo
, but not the specific type parameters ahead of time. That’s okay, this works.
3-element Vector{Foo}:
Foo{Int64, Int64}(1, 2)
Foo{Float64, Int64}(1.0, 2)
Foo{Float64, Float64}(1.0, 2.0)
Now if I extend this with another wrapper type
struct Bar{T}
a::T
end
The following fails and I’m unclear why
v = Bar{Foo}[]
push!(v, Bar(a))
ERROR: MethodError: Cannot
convert
an object of type
Bar{Foo{Int64, Int64}} to an object of type
Bar{Foo}
Closest candidates are:
convert(::Type{T}, ::T) where T at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/essentials.jl:218
Bar{T}(::Any) where T at REPL[11]:2
I’m not clear why the second fails and the first does not. Any tips on how to get this sort of pattern working?