# How do I implement array indexing?

**URL:** https://discourse.julialang.org/t/how-do-i-implement-array-indexing/51506
**Category:** General Usage
**Created:** [December 9, 2020, 9:04am UTC](https://discourse.julialang.org/t/how-do-i-implement-array-indexing/51506 "2020-12-09T09:04:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![minetest2048](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/minetest2048/32/45961_2.png) [@minetest2048](https://discourse.julialang.org/u/minetest2048)
#### Post date: [December 9, 2020, 9:04am UTC](https://discourse.julialang.org/t/how-do-i-implement-array-indexing/51506/1 "2020-12-09T09:04:36Z")

</div>

I have this array type:

```julia
struct Delay{T}
	head_idx::Int
	arr::Vector{T}
	function Delay{T}(length::Int) where T
		if ispow2(length)
			arr = zeros(T,length)
			new(0, arr)
		else
			throw(ArgumentError("Length is not power of 2"))
		end
	end
end

```

and I implemented the `getindex` function

```julia
function getindex(A::Delay{T},idx) where T
	if idx > 0
		throw(ArgumentError("Only accepts negative indexes"))
	else
		return 1
	end
end

```

then I define `test` as

```julia
test = Delay{Float32}(8)

```

When I try to use `test[1]` it errors with `ERROR: MethodError: no method matching getindex(::Delay{Float32}, ::Int64)` but if I use `getindex` function directly it works

```julia
julia> getindex(test,-1)
1

```

What happened?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 9, 2020, 9:15am UTC](https://discourse.julialang.org/t/how-do-i-implement-array-indexing/51506/2 "2020-12-09T09:15:52Z")

</div>

You need to explicitly import the `Base.getindex` function:

```julia

julia> function Base.getindex(A::Delay{T},idx) where T
               if idx > 0
                       throw(ArgumentError("Only accepts negative indexes"))
               else
                       return 1
               end
       end

julia> test[-1]
1

```

Equivalent to:

```julia
import Base.getindex
function getindex(...) ...

```

---

<div class="post-metadata">

### Author: ![minetest2048](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/minetest2048/32/45961_2.png) [@minetest2048](https://discourse.julialang.org/u/minetest2048)
#### Post date: [December 9, 2020, 9:41am UTC](https://discourse.julialang.org/t/how-do-i-implement-array-indexing/51506/3 "2020-12-09T09:41:46Z")

</div>

It works! Thanks

Generalizing, what functions should I implement for my array type? In Python, there is this: [3. Data model — Python 3.10.6 documentation](https://docs.python.org/3/reference/datamodel.html#emulating-container-types) which states which methods that I should implement so that my class acts like a list, is there a documentation like this for Julia?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 9, 2020, 10:07am UTC](https://discourse.julialang.org/t/how-do-i-implement-array-indexing/51506/4 "2020-12-09T10:07:54Z")

</div>

The page to read is this one, I think: [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/#Indexing)

Depending on what you need to do with your data you might need only the indexing part, or something else.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [December 9, 2020, 10:13am UTC](https://discourse.julialang.org/t/how-do-i-implement-array-indexing/51506/5 "2020-12-09T10:13:18Z")

</div>

If you do want it to be an array type, look at the [AbstractArray](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array) section.
