Custom arrays for dispatch

What I ended up doing was creating a collection of types with no fields under an abstract Domain type to indicate the sort qualities my arrays represented, and then making one parametric array type which took a Domain subtype as a parameter. Something like this:

abstract type Domain end
struct RealPhase <: Domain end
struct Amplitude <: Domain end
struct Intensity <: Domain end

struct Field{S<:Domain,T<:Number,N} <: AbstractArray{T,N}
    data::AbstractArray{T,N}
end
Field{S}(array::AbstractArray{T,N}) where {S<:Domain,T<:Number,N} = Field{S,T,N}(array)

# Minimal methods to make these types function as arrays.  
# All arithmetic works on a Field, though the output will be some sort of array, and not a Field. 
Base.size(f::Field) = size(f.data)
Base.getindex(f::Field, i::Int) = getindex(f.data,i)
Base.setindex!(f::Field, v, i::Int) = setindex!(f.data, v, i)
Base.IndexStyle(::Type{<:Field})= IndexLinear()

This seems to me the slickest way to achieve what I wanted, as I don’t need to define any arithmetic for Domain types, and I only need to define array methods once.

1 Like