# DeepGetfield.jl

**URL:** <https://discourse.julialang.org/t/deepgetfield-jl/11121>\
**Category:** General Usage\
**Tags:** announcement\
**Created:** [May 24, 2018, 5:59am UTC](https://discourse.julialang.org/t/deepgetfield-jl/11121 "2018-05-24T05:59:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 24, 2018, 5:59am UTC](https://discourse.julialang.org/t/deepgetfield-jl/11121/1 "2018-05-24T05:59:00Z")

</div>

I developed a small utility package  
[DeepGetfield.jl](https://github.com/baggepinnen/DeepGetfield.jl)  
that provides the macro `@deep composite.field` which makes `getfield` look through a `composite` type recursively to find the specified `field` somewhere deep inside. I use it when I’ve run many experiments resulting in arrays of structs with arrays and structs inside, to find a particular field deep inside for analysis.

Don’t hesitate to criticise the implementation if you find it interesting!

# Usage example

```julia-auto
Pkg.clone("https://github.com/baggepinnen/DeepGetfield.jl")
using DeepGetfield

struct A
    x::Int
    y
    z
end

struct B
    xx
    yy
end

struct C
    zz
end

julia> a = A(2, 1., B(10., [C(1), C(2)])) # Create complex composite type

julia> dump(a) # Vizualize the structure and contents of a
A
  x: Int64 2
  y: Float64 1.0
  z: B
    xx: Float64 10.0
    yy: Array{C}((2,))
      1: C
        zz: Int64 1
      2: C
        zz: Int64 2

julia> getterfun = @deepf a.yy # This function acts as deep_getfield(a,:yy)

julia> getterfun(a) # Get field yy, equivalent to a.z.yy
2-element Array{C,1}:
 C(1)
 C(2)

julia> @deep a.yy # Evaluate immediately without getting a function
2-element Array{C,1}:
 C(1)
 C(2)

julia> getterfun = @deepf a.zz # Getter for field zz, equivalent to getfield.(a.z.yy, :zz)

julia> getterfun(a) == [1,2] == getfield.(a.z.yy, :zz)
true

```

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [May 24, 2018, 6:41am UTC](https://discourse.julialang.org/t/deepgetfield-jl/11121/2 "2018-05-24T06:41:48Z")

</div>

What does it do in case of an ambiguity?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 24, 2018, 6:51am UTC](https://discourse.julialang.org/t/deepgetfield-jl/11121/3 "2018-05-24T06:51:02Z")

</div>

- If several fields inside the structure have the same name, the first field found (depth first) is returned and others ignored.
- Arrays are looked through, but it is assumed that all elements in the array have the same field names. Only the first element is looked at during search. If the field is found inside an {Array / Array{Arrays}}, then the result will have that same structure

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 24, 2018, 7:33am UTC](https://discourse.julialang.org/t/deepgetfield-jl/11121/4 "2018-05-24T07:33:16Z")

</div>

> [@baggepinnen](#):
>
> the first field found (depth first)

Wouldn’t breadth first be more natural?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 24, 2018, 7:49am UTC](https://discourse.julialang.org/t/deepgetfield-jl/11121/5 "2018-05-24T07:49:06Z")

</div>

Yeah in most circumstances it probably would. Depth first is easier to implement though and since that solved my problems I haven’t reached any further yet 🙈

---

<div class="post-metadata">

**Author:** ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)\
**Post date:** [May 24, 2018, 7:53am UTC](https://discourse.julialang.org/t/deepgetfield-jl/11121/6 "2018-05-24T07:53:09Z")

</div>

Do you plan in future to support also getproperty accesor? That one is dynamic and that would be a killer usecase for accessing python classes or xml elements on the REPL.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 24, 2018, 8:54am UTC](https://discourse.julialang.org/t/deepgetfield-jl/11121/7 "2018-05-24T08:54:39Z")

</div>

I probably will if I find it useful
