I’m writing an application where I’ve found it convenient to dispatch on the parameters of some structs. Something like the following:
abstract type MyType end
abstract type A <: MyType end
abstract type B <: MyType end
struct MyStruct1{MT<:MyType,T}
val1 :: T
val2 :: T
# ...
end
MyStruct1{MT}(x,y) where {MT<:MyType} = MyStruct1{MT,typeof(x)}(x,y)
struct MyStruct2{MT<:MyType,T}
ab :: T
cd :: T
# ...
end
MyStruct2{MT}(x,y) where {MT<:MyType} = MyStruct2{MT,typeof(x)}(x,y)
func1(foo::MyStruct1{A}) = foo.val1 + foo.val2
func1(foo::MyStruct1{B}) = foo.val1 * foo.val2
func2(foo::MyStruct2{A}) = 42
func2(foo::MyStruct2{B}) = 666
This allows me to dispatch on the parameter MyType
as follows:
julia> fooA = MyStruct1{A}(1,2)
julia> fooB = MyStruct1{B}(1,2)
julia> fooC = MyStruct2{A}(1,2)
julia> fooD = MyStruct2{B}(1,2)
julia> func1(fooA)
3
julia> func1(fooB)
2
julia> func2(fooC)
42
julia> func2(fooD)
666
This seems to be working fine for me, and I’ve noticed that there have been similar questions, like here: Dispatch function based on value of parametric type
However, I’ve noticed the section on the manual warning on the dangers of abusing multiple dispatch and I’m now wondering if this applies to my construction above… My actual use case has much more complicated functions and each struct has a lot more elements. Is there anything I should be concerned with in the construction above?