# Defining custom getindex method

**URL:** <https://discourse.julialang.org/t/defining-custom-getindex-method/54038>\
**Category:** New to Julia\
**Created:** [January 27, 2021, 6:59am UTC](https://discourse.julialang.org/t/defining-custom-getindex-method/54038 "2021-01-27T06:59:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zachary\_Winkeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachary_winkeler/32/21424_2.png) [@Zachary\_Winkeler](https://discourse.julialang.org/u/Zachary_Winkeler)\
**Post date:** [January 27, 2021, 6:59am UTC](https://discourse.julialang.org/t/defining-custom-getindex-method/54038/1 "2021-01-27T06:59:15Z")

</div>

Hi all, just started learning Julia, and I think I’m missing something really obvious here. I’m trying to make a type indexable, but I get an error when using the […] syntax. Specifically, if g is an instance of my type, getindex(g,1) works fine, but g[1] gives me an error. The getindex I’ve defined seems like it should be suitably general to match this type, and this is especially weird to me because the documentation says that g[1] should be compiled as getindex(g,1). Example:

```julia
struct Graded{S,T}
    summands :: Dict{S,T}
end

function getindex(x::Graded{S,T}, k::S) where {S,T}
    return x.summands[k]
end

d = Dict{Int,Array{Int}}([0=>[], 1=>[1,2,3], 2=>[10,11,12]])
g = Graded{Int,Array{Int}}(d)

print(getindex(g,1))
print(g[1])

```

The last line gives the error:

```julia
 ERROR: LoadError: MethodError: no method matching getindex(::Graded{Int64,Array{Int64,N} where N}, ::Int64)

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [January 27, 2021, 7:35am UTC](https://discourse.julialang.org/t/defining-custom-getindex-method/54038/2 "2021-01-27T07:35:14Z")

</div>

The problem here is that `g[1]` always calls `Base.getindex`, while your function definition has in fact made a new function in your current module. The solution is to always qualify functions you are trying to extend, when the original definition is in another module:

```julia
julia> @which getindex
Main

julia> methods(getindex) # just one:
# 1 method for generic function "getindex":
[1] getindex(x::Graded{S, T}, k::S) where {S, T} in Main at REPL[523]:1

julia> methods(Base.getindex) |> length
517

julia> function Base.getindex(x::Graded{S,T}, k::S) where {S,T}
           return x.summands[k]
       end

julia> print(g[1])
[1, 2, 3]

```

It’s a little strange to think that you could mean anything else. But the general rule makes more sense – for instance it lets you use words like `sum` or `sign` as variable names within a function, so long as you assign to them before reading from them, without worrying that these also exist as functions exported by Base.

---

<div class="post-metadata">

**Author:** ![Zachary\_Winkeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachary_winkeler/32/21424_2.png) [@Zachary\_Winkeler](https://discourse.julialang.org/u/Zachary_Winkeler)\
**Post date:** [January 27, 2021, 7:38am UTC](https://discourse.julialang.org/t/defining-custom-getindex-method/54038/3 "2021-01-27T07:38:07Z")

</div>

Perfect, thank you!
