Add Base operator function to custom struct automatically

Here it falls back to + defined on AbstractArray, because AxisArray is subtyped based on AbstractArray as shown below (from AxisArray impl)

struct AxisArray{T,N,D,Ax} <: AbstractArray{T,N}
    data::D  # D <:AbstractArray, enforced in constructor to avoid dispatch bugs (https://github.com/JuliaLang/julia/issues/6383)
    axes::Ax # Ax<:NTuple{N, Axis}, but with specialized Axis{...} types
    AxisArray{T,N,D,Ax}(data::AbstractArray{T,N}, axs::Tuple{Vararg{Axis,N}}) where {T,N,D,Ax} = new{T,N,D,Ax}(data, axs)
end

So, it works if you also implement your type to be a subtype of AbstractArray (iff it is feasible in your case…!!)

1 Like