# Get any object properties by name

**URL:** <https://discourse.julialang.org/t/get-any-object-properties-by-name/76070>\
**Category:** General Usage\
**Created:** [February 9, 2022, 11:40am UTC](https://discourse.julialang.org/t/get-any-object-properties-by-name/76070 "2022-02-09T11:40:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [February 9, 2022, 11:40am UTC](https://discourse.julialang.org/t/get-any-object-properties-by-name/76070/1 "2022-02-09T11:40:50Z")

</div>

Hello, I need to get object property values by their names.  
The order of the values must match the order of the property names.  
Can it be done better?

```julia
struct TestObject
    a
    b
    c
end

object = TestObject(1,2,3)

function getproperties(object::T, properties::Union{Symbol, Tuple{Vararg{Symbol}}, AbstractVector{Symbol}}) where T
    values = []
    for property in properties
        push!(values, getproperty(object, property))
    end
    return values
end

getproperties(object, (:a,:c,:b))

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 9, 2022, 11:54am UTC](https://discourse.julialang.org/t/get-any-object-properties-by-name/76070/2 "2022-02-09T11:54:00Z")

</div>

As a generator

```julia
getproperties(object, properties) = 
    (getproperty(object, property) for property in properties)
collect(getproperties(object, (:a,:c,:b)))

```

or comprehension

```julia
getproperties(object, properties) = 
    [getproperty(object, property) for property in properties]
getproperties(object, (:a,:c,:b))

```

? Quick benchmark:

```julia
@btime getproperties($Ref(object)[], (:a,:c,:b)) 

getproperties(object, properties) = 
    (getproperty(object, property) for property in properties)
@btime collect(getproperties($Ref(object)[], (:a,:c,:b))) 

getproperties(object, properties) = 
    [getproperty(object, property) for property in properties]
@btime getproperties($Ref(object)[], (:a,:c,:b))

```

yields

```julia
  293.243 ns (7 allocations: 288 bytes)
  319.383 ns (8 allocations: 400 bytes)
  242.857 ns (7 allocations: 288 bytes)

```

---

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [February 9, 2022, 12:07pm UTC](https://discourse.julialang.org/t/get-any-object-properties-by-name/76070/3 "2022-02-09T12:07:59Z")

</div>

from the heart)

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [February 9, 2022, 12:09pm UTC](https://discourse.julialang.org/t/get-any-object-properties-by-name/76070/4 "2022-02-09T12:09:55Z")

</div>

Why not just broadcasting getproperty:

```julia
getproperty.(object, (:a,:c,:b))

```

---

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [February 9, 2022, 12:10pm UTC](https://discourse.julialang.org/t/get-any-object-properties-by-name/76070/5 "2022-02-09T12:10:38Z")

</div>

doesn’t work like that

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 9, 2022, 12:18pm UTC](https://discourse.julialang.org/t/get-any-object-properties-by-name/76070/6 "2022-02-09T12:18:41Z")

</div>

Just [define a `broadcastable` method](https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting):

```julia
Base.broadcastable(o::TestObject) = Ref(o)

```

and then you can do

```julia
julia> getproperty.(object, (:a,:c,:b))
(1, 3, 2)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 9, 2022, 12:45pm UTC](https://discourse.julialang.org/t/get-any-object-properties-by-name/76070/7 "2022-02-09T12:45:00Z")

</div>

Note also that in Julia 1.7 you can do [property destructuring](https://julialang.org/blog/2021/11/julia-1.7-highlights/#property_destructuring):

```julia
julia> (; a,c,b) = TestObject(1, 2, 3);

julia> a,c,b
(1, 3, 2)

```
