Hello everyone, I’m having a hard time figuring out how to implement arrays with custom indices.

I’ve read all the existing documentation out there, but it’s often poor and lacking of information, especially examples.

I’m trying to implement a simple array (extending AbstractArray) with indices ranging between two custom values (‘lb’ and ‘ub’ below), which can also be negative numbers.

struct MyType{T} <: AbstractArray{T,1}

data::Array{T,1}

dim::Int #Number of components of ‘data’

lb::Int #lower bound of indices

ub::Int #Upper bound of indices

end

Now I’m really not sure how to redefine the array functions, in particular:

- Shall I redefine show? Some say I shouldn’t, but why?
- What about getindex() and setindex!()? No tutorial shows an example of their redefinition with custom indices, so I assume that they remain unchanged, but it seems strange.

Maybe I should use ‘1+i-a.lb’ instead of ‘i’ as index of data?

Since I don’t know much about this, here’s the rest of my code until now, please suggest me what to change to make it work properly.

MyType{T}(::Type{T}, n::Int, lb::Int = 0) = MyType(Array{T,1}(n), n, lb, lb+n-1);

MyType(n::Int) = MyType(Float64, n);

Base.size(a::MyType) = (a.dim,)

Base.IndexStyle(::Type{<:MyType}) = IndexLinear()

Base.similar(a::MyType, ::Type{T}, d::Dims) where {T} = MyType(T, d[1])

Base.getindex(a::MyType, i::Int) = a.data[i]

Base.setindex!(a::MyType, v, i::Int) = (a.data[i] = v)

Base.indices(a::MyType) = map(URange, a.lb, a.ub)

Thanks in advance!