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
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> 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