# Dispatching custom \`getindex\` method

**URL:** <https://discourse.julialang.org/t/dispatching-custom-getindex-method/14601>\
**Category:** General Usage\
**Created:** [September 6, 2018, 12:29am UTC](https://discourse.julialang.org/t/dispatching-custom-getindex-method/14601 "2018-09-06T00:29:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![00vareladavid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00vareladavid/32/23521_2.png) [@00vareladavid](https://discourse.julialang.org/u/00vareladavid)\
**Post date:** [September 6, 2018, 12:29am UTC](https://discourse.julialang.org/t/dispatching-custom-getindex-method/14601/1 "2018-09-06T00:29:21Z")

</div>

I have a `Vector`, say `xs`. I also have a `const CustomType = Int`. Basically I want to index `xs` with `CustomType` values. Easy enough with `xs[custom_mapping(i)]`. Is there a way to leverage the type system so I can write `xs[i]` where `i::CustomType`? It seems I can not use the “const” method, because `CustomType` will be evaluated to `Int`: which means an eventual `getindex(Vector, Int)` method call (which is not what I want).

The only way I can come up with is wrapping the vector in a `struct`. Is there a more idiomatic/clean way? If I do use a `struct`, is there a performance penalty? Maybe I’m missing something obvious…

I would really like to use `x[i]` syntax since my code is indexing specific locations pretty heavily. Thanks in advance for any feedback!

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [September 6, 2018, 12:57am UTC](https://discourse.julialang.org/t/dispatching-custom-getindex-method/14601/2 "2018-09-06T00:57:20Z")

</div>

You can either use what you figured out, wrapping your vector in a struct and defining indexing for it, or you can wrap `Int` in a struct:

```julia
struct CustomType
    x::Int 
end

```

And define a `getindex(x::AbstractArray,i::CustomType)` method.
