Skip to content

Instantly share code, notes, and snippets.

Last active Jan 24, 2019
What would you like to do?
struct Name{T}
struct Arr{T, N} <: AbstractArray{T, N}
data::Array{T, N}
Base.size(A::Arr) = size(
Base.axes(A::Arr) = axes(
Base.getindex(A::Arr, i::Int) =[i]
Base.getindex(A::Arr{T, N}, I::Vararg{Int, N}) where {T, N} =[I...]
resolve_index(A, i) = i
resolve_index(A, i::Name) =
function Base.to_indices(A::Arr, inds, I::Tuple{Any, Vararg{Any}})
I′ = Tuple(resolve_index(A, i) for i in I)
to_indices(, inds, I′)
function Base.getindex(A::Arr{T, N}, I::Vararg{Any}) where {T, N}
# Resolve any names to indices into the underlying array
I′ = to_indices(A, I)[I′...]
a = Arr([1 2; 3 4])
a[2] # => 2
a[Name(2)] # => 2
# You may need to `]add`
using InvertedIndices
a[Not(2)] # => error
a[Not(Name(2))] # => error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment