Reputation: 3789
Suppose I have an abstract base class:
abstract type B end
and many concrete subtypes:
struct C1{T,V} <: B end
struct C2{T,V} <: B end
...
how to define a method for all subtypes in an elegant way?
result_type(::C1{T,V}) = T
result_type(::C2{T,V}) = T
...
The subtypes
function doesn't seem to contain the parameter types T
and V
that I am trying to query, is there a clean solution?
for C in subtypes(B)
# how to define result_type for C?
end
Upvotes: 1
Views: 287
Reputation: 19162
Just add the type parameter to the abstract type.
abstract type B{T,V} end
struct C1{T,V} <: B{T,V} end
struct C2{T,V} <: B{T,V} end
result_type(::B{T,V}) where {T,V} = T
Upvotes: 5