# Implementing the AbstractArray interface

**URL:** <https://discourse.julialang.org/t/implementing-the-abstractarray-interface/17567>\
**Category:** New to Julia\
**Created:** [November 15, 2018, 5:36pm UTC](https://discourse.julialang.org/t/implementing-the-abstractarray-interface/17567 "2018-11-15T17:36:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oatlzzvztd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oatlzzvztd/32/3285_2.png) [@oatlzzvztd](https://discourse.julialang.org/u/oatlzzvztd)\
**Post date:** [November 15, 2018, 5:36pm UTC](https://discourse.julialang.org/t/implementing-the-abstractarray-interface/17567/1 "2018-11-15T17:36:57Z")

</div>

First of all, I’m still running v0.6.4 (-\_-).

I’m specifically trying to write a subtype of AbstractArray with a non-standard data structure backing it. I’d like to override getindex specifically in the case of slices (e.g., index varargs containing either single integers or Colons), but I’m not sure what signature of getindex/setindex! I need to override.

base/abstractarray.jl says:

```julia
## Approach:
# We only define one fallback method on getindex for all argument types.
# That dispatches to an (inlined) internal _getindex function, where the goal is
# to transform the indices such that we can call the only getindex method that
# we require the type A{T,N} <: AbstractArray{T,N} to define; either:
# getindex(::A, ::Int) # if IndexStyle(A) == IndexLinear() OR
# getindex{T,N}(::A{T,N}, ::Vararg{Int, N}) # if IndexCartesian()
# If the subtype hasn't defined the required method, it falls back to the
# _getindex function again where an error is thrown to prevent stack overflows.

```

But supplying the second method only seems to net me scalar indexing. I’d even like full non-scalar multidimensional indexing.

I looked at StaticArrays.jl as well, but their getindex/setindex! functions are generated which has the side-effect of making it harder for me to read.

If someone could point me in the right direction, I’d greatly appreciate it.

EDIT: Just in case it’s unclear, I’d like the non-scalar getindex to return a CustomArray as well. The data structure stores some bookkeeping that I’d like to persist across slices.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 15, 2018, 5:41pm UTC](https://discourse.julialang.org/t/implementing-the-abstractarray-interface/17567/2 "2018-11-15T17:41:03Z")

</div>

That comment is describing the strategy on how we _implement_ the backbone architecture that supports the AbstractArray interface, not how users should extend it themselves.

Have you read [Interfaces · The Julia Language](https://docs.julialang.org/en/v0.6.4/manual/interfaces/#man-interface-array-1) ?

---

<div class="post-metadata">

**Author:** ![oatlzzvztd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oatlzzvztd/32/3285_2.png) [@oatlzzvztd](https://discourse.julialang.org/u/oatlzzvztd)\
**Post date:** [November 15, 2018, 5:42pm UTC](https://discourse.julialang.org/t/implementing-the-abstractarray-interface/17567/3 "2018-11-15T17:42:45Z")

</div>

> [@mbauman](#):
>
> Have you read [Interfaces · The Julia Language](https://docs.julialang.org/en/v0.6.4/manual/interfaces/#man-interface-array-1) ?

Yes; it tells me to override `getindex(A, I...)`. I don’t understand what the type of `I` is there.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 15, 2018, 5:52pm UTC](https://discourse.julialang.org/t/implementing-the-abstractarray-interface/17567/4 "2018-11-15T17:52:50Z")

</div>

The only methods that are required to get your array working (in a bare-bones manner) are `getindex` with `Int` index(es) and `size` (see the required methods section of that table). Everything beyond that is optional customization. You can also completely customize `getindex(A::CustomArray, I::Any...)` (so it’s an optional method to override) but of course then you’re on your own in implementing all the indexing behaviors we support. So there are the other optional methods that can help you tweak things here and there.

The tweak I think you’re looking for is `similar`. Check out the toy `SparseArray` example — I think that does what you’re looking for: it implements scalar (`Int`) indexing and indexed assignment and then defines `similar` such that non-scalar indexing returns `SparseArray`s instead of `Array`.

---

<div class="post-metadata">

**Author:** ![oatlzzvztd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oatlzzvztd/32/3285_2.png) [@oatlzzvztd](https://discourse.julialang.org/u/oatlzzvztd)\
**Post date:** [November 15, 2018, 7:11pm UTC](https://discourse.julialang.org/t/implementing-the-abstractarray-interface/17567/5 "2018-11-15T19:11:50Z")

</div>

I see. That did the trick. Thanks!
