# Is it possible to implement own \`.\<tab\>\` autocomplete for \`getindex\` in REPL (without AbstractDict)

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-implement-own-tab-autocomplete-for-getindex-in-repl-without-abstractdict/96552>\
**Category:** General Usage\
**Tags:** question, repl\
**Created:** [March 24, 2023, 11:33am UTC](https://discourse.julialang.org/t/is-it-possible-to-implement-own-tab-autocomplete-for-getindex-in-repl-without-abstractdict/96552 "2023-03-24T11:33:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [March 24, 2023, 11:33am UTC](https://discourse.julialang.org/t/is-it-possible-to-implement-own-tab-autocomplete-for-getindex-in-repl-without-abstractdict/96552/1 "2023-03-24T11:33:36Z")

</div>

I have some datatype, for which I would like to have autocomplete for `getindex/setindex!`, e.g. `myData[.<tab>` should give me all the `keys(myData)`.

So far, I get that I can subtype `AbstractDict{Symbol,Any}` to archive that. But is it also possible otherwise?

For example, it seems that I have to implement `iterate` for `AbstractDict`, whereas for my type there is no reasons for `iterate`.

Here is a minimal (working) example

```julia
struct Keyholder{T<:NamedTuple} <: AbstractDict{:Symbol, Any}
    data::T
end

Base.getindex(k::Keyholder, sym::Symbol) = getproperty(k.data, sym)
Base.iterate(k::Keyholder) = iterate(pairs(k.data))
Base.iterate(k::Keyholder, state) = iterate(pairs(k.data), state)

Base.keys(k::Keyholder) = keys(k.data)
Base.keytype(::Keyholder) = :Symbol
Base.length(k::Keyholder) = length(keys(k))

k = Keyholder( (;A = 1, B = 2) )
# k[:<tab> gives :A :B

```

PS: In my real application, I maybe would like to not be a subtype of `AbstractDict`, that’s why I care about this question 😉 Of course, giving up autocomplete is an easy solution which I would accept.

---

<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:** [March 24, 2023, 11:49am UTC](https://discourse.julialang.org/t/is-it-possible-to-implement-own-tab-autocomplete-for-getindex-in-repl-without-abstractdict/96552/2 "2023-03-24T11:49:31Z")

</div>

I think what you want is to overload `propertynames` for your type.

```julia
julia> struct A
           a::Int
       end

julia> Base.getproperty(a::A, s::Symbol) = s == :x ? getfield(a,:a) : nothing

julia> Base.propertynames(a::A) = (:x,)

julia> a.x
1

julia> a.a

julia>

```

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [March 24, 2023, 11:53am UTC](https://discourse.julialang.org/t/is-it-possible-to-implement-own-tab-autocomplete-for-getindex-in-repl-without-abstractdict/96552/3 "2023-03-24T11:53:26Z")

</div>

Mh, I did that before and somehow I didn’t like it in my setup. I think one reason is that the `struct` I would like to autocomplete, might be user facing.  
And that could lead to confusion for people who don’t know `getfield`.

But yes, `propertynames` is much easier!

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [March 24, 2023, 11:59am UTC](https://discourse.julialang.org/t/is-it-possible-to-implement-own-tab-autocomplete-for-getindex-in-repl-without-abstractdict/96552/4 "2023-03-24T11:59:40Z")

</div>

Looking at your solution, I actually got the idea to append `getproperty`. That could work well, e.g.  
`Base.propertynames(a::A) = (:x, fieldnames(A)...)` and `getproperty` accordingly. Thanks!
