假设我有一个抽象基类:
abstract type B end
以及许多具体的子类型:
struct C1{T,V} <: B end
struct C2{T,V} <: B end
...
如何以优雅的方式为所有子类型定义方法?
result_type(::C1{T,V}) = T
result_type(::C2{T,V}) = T
...
该subtypes
功能似乎并没有包含的参数类型T
和V
,我想询问,有没有一个干净的解决方案?
for C in subtypes(B)
# how to define result_type for C?
end
只需将类型参数添加到抽象类型即可。
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句