# Recommended approach for getproperty shadowing

**URL:** <https://discourse.julialang.org/t/recommended-approach-for-getproperty-shadowing/18312>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 5, 2018, 11:50am UTC](https://discourse.julialang.org/t/recommended-approach-for-getproperty-shadowing/18312 "2018-12-05T11:50:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 5, 2018, 11:50am UTC](https://discourse.julialang.org/t/recommended-approach-for-getproperty-shadowing/18312/1 "2018-12-05T11:50:30Z")

</div>

Suppose I have a wrapper type that has some contents I would like to index with symbols, for both `getindex` and `getproperty` syntaxes (if you want a concrete example, think of a `DataFrame`).

However, this could be problematic when the symbol names coincide with the field name.

What’s the recommended way of dealing with this? Eg fall back to `getfield` for the fields, perhaps making this more convenient with an accessor? Eg

```julia
struct Foo{T <: NamedTuple}
    contents::T
end

contents(f::Foo) = getfield(f, :contents)

Base.getproperty(f::Foo, name::Symbol) = getproperty(contents(f), name)
Base.propertynames(f::Foo) = propertynames(contents(f))

```

then

```julia
julia> f = Foo((a = 1, b = 2, contents = 3))
Foo{NamedTuple{(:a, :b, :contents),Tuple{Int64,Int64,Int64}}}((a = 1, b = 2, contents = 3))

julia> f.a
1

julia> f.b
2

julia> f.contents
3

```

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [December 5, 2018, 12:36pm UTC](https://discourse.julialang.org/t/recommended-approach-for-getproperty-shadowing/18312/2 "2018-12-05T12:36:34Z")

</div>

In Gtk.jl for [GObjects](https://github.com/JuliaGraphics/Gtk.jl/blob/17bfda1ea78a511cd39d58de56c09cadd93262fb/src/GLib/gvalues.jl#L185) we return the field if it exists otherwise we return the Gtk property. I haven’t used it much but it seems to work fine in practice. That said in Gtk you also need to specify the type of the property you want to access, so it’s clear syntactically if the intent is to access a field or a property (otherwise it would be a bit awkward):

```julia
foo.bar #field
foo.bar[Int] #Gtk property of type Int

```

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [December 5, 2018, 3:22pm UTC](https://discourse.julialang.org/t/recommended-approach-for-getproperty-shadowing/18312/3 "2018-12-05T15:22:59Z")

</div>

```julia
struct Foo{T <: NamedTuple}
    asdfasdfasdfwqefasdfvjwlqewazdf::T
end

```

Problem solved.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [December 5, 2018, 11:49pm UTC](https://discourse.julialang.org/t/recommended-approach-for-getproperty-shadowing/18312/4 "2018-12-05T23:49:14Z")

</div>

@Tamas_Papp FWIW, [PyCall.jl is planning to add](https://github.com/JuliaPy/PyCall.jl/pull/517) the interface implemented as in your example code (i.e., property lookup is completely forwarded and field lookup is done by a custom accessor wrapping `getfield`).
