# "forwarding" of properties

**URL:** <https://discourse.julialang.org/t/forwarding-of-properties/14116>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 27, 2018, 9:15am UTC](https://discourse.julialang.org/t/forwarding-of-properties/14116 "2018-08-27T09:15:40Z")\
**Posts on this page:** 2\
**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:** [August 27, 2018, 9:15am UTC](https://discourse.julialang.org/t/forwarding-of-properties/14116/1 "2018-08-27T09:15:40Z")

</div>

I am very excited about `getproperty` & friends in 1.0, as it would make a particular package I am working on have a much simpler API. I could not find a lot of guidance about it though at the moment, so I am learning from the examples (eg in `LinearAlgebra`).

I am wondering about the possibility of “forwarding” all properties which are not fields to a particular field. MWE:

```julia
import Base: propertynames, getproperty

"Subtypes have a field `x`, used for properties not in `fieldnames`."
abstract type ForwardOnX end

propertynames(w::ForwardOnX) = (fieldnames(w)..., propertynames(w.x)...)

function getproperty(w::TX, name::Symbol) where {TX <: ForwardOnX}
    if name ∈ fieldnames(TX)
        getfield(w, name)
    else
        getproperty(w.x, name)
    end
end

struct Test0{A}
    a::A
end

struct Test1{X,B} <: ForwardOnX
    x::X
    b::B
end

t0 = Test0(:a)
t1 = Test1(t0, :b)
t0.a # :a
t1.a # :a
t1.b # :b, the relevant field
t1.x # the relevant field

```

Is this an OK pattern? I am hoping it would compile to reasonably efficient code with constant folding. Anything I could do to improve it?

---

<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:** [August 27, 2018, 9:18am UTC](https://discourse.julialang.org/t/forwarding-of-properties/14116/2 "2018-08-27T09:18:37Z")

</div>

PS: probably

```julia
propertynames(w::ForwardOnX) = unique((fieldnames(w)..., propertynames(w.x)...))

```

would be nicer.
