I would like to make my custom Type behave like a numeric type. Ideally I would specify a custom convert()
function that would do some smart computations to summarize the type’s fields as a single value. What is the cleanest and most design friendly way to do this? I got it working but it seems like some kind of inheritance method would be more reusable.
julia> struct Ratio
num::Float64
end
julia> using Statistics
julia> Ratio[Ratio(1), Ratio(2), Ratio(3)]
3-element Vector{Ratio}:
Ratio(1.0)
Ratio(2.0)
Ratio(3.0)
julia> Statistics.mean(ans)
ERROR: MethodError: no method matching /(::Ratio, ::Int64)
[...]
julia> Statistics.mean(ratios::Vector{Ratio}) = Statistics.mean(getfield.(ratios, :num))
julia> Statistics.mean(ratios)
2.0
Ideally I don’t want to create my own duplicate method for every function in Statistics and other modules, but just some way that those functions can see my value as a numeric type natively.