I am struggling to correctly set up a parametric struct.
This is most likely a very stupid question, however, I really don’t understand what is wrong with my code so please be gentle on me…
I want to create a struct which has a field that can be of various types which all are subtypes of one abstract type.
I created the following dummy example which is not working.
Can anyone explain to me
- How to fix this code and
- Why the enum version further below is working
abstract type Bar end
struct SpecificBar <: Bar end
mutable struct Foo{T <: Bar}
x::Array{Tuple}
type::T
end
Foo([(1,1)], SpecificBar()) # returns error
I get the following error: ERROR: LoadError: MethodError: no method matching Foo(::Array{Tuple{Int64,Int64},1}, ::SpecificBar) Closest candidates are: Foo(::Array{Tuple,N} where N, ::T) where T<:Bar
My interpretation of the error message is that the issue is not with the field type but with the field x. If this is the case it is surprising to me that the below code using enum instead of abstract type works.
@enum BarEnum Bar1 Bar2
mutable struct Foo2
x::Array{Tuple}
type::BarEnum
end
Foo2([(1,1)], Bar1)
Given I want to use multiple dispatch I feel it is better to use an abstract type instead of enums. Is this even a valid argument?
Thanks for your help!