Recipe for custom array types with metadata bound to it

array
data_structures

#1

I want to implement custom array type with some additional fields to store useful metadata information bound to that array. But, similar to OffsetArrays.jl, I want to implement it for any AbstractArray subtype (simple array, AxisArray, StructArray, CircularBuffer, etc.).

To do that, I should write something like OffsetArray source:

mutable struct MyArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N}
    array::AA
    useful_data1::Int64
    useful_data2::String
    # and maybe another useful data fields
end

Then I should reimplemet all AbstractArray methods in the form that:

size(X::MyArray) = size(X.array)
getindex(X::MyArray, i) = getindex(X.array, i)

And so on for every abstract array interface method, right? Or even for some other interfaces too (indexing, iterator)?

It would be simpler if I had some common recipe to do that in one row for any particular custom array type with different metadata fields, without having to rewrite all those methods. How can I do that?


Type parameter transformation, StaticArray with blade indexing
#2

This was discussed in quite some detail here.