# How to use \`getproperty\`/\`setproperty!\`?

**URL:** <https://discourse.julialang.org/t/how-to-use-getproperty-setproperty/16033>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 8, 2018, 5:27pm UTC](https://discourse.julialang.org/t/how-to-use-getproperty-setproperty/16033 "2018-10-08T17:27:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 8, 2018, 5:27pm UTC](https://discourse.julialang.org/t/how-to-use-getproperty-setproperty/16033/1 "2018-10-08T17:27:55Z")

</div>

The recommended way to implement `getproperty(value, name::Symbol)` is by making as many branches as required to check what `name` is?  
This seems inefficient. Or is the compiler able to eliminate the branching when it sees `a.b` (hence knows the actual symbol used)?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 8, 2018, 5:38pm UTC](https://discourse.julialang.org/t/how-to-use-getproperty-setproperty/16033/2 "2018-10-08T17:38:46Z")

</div>

Yes, it can generally eliminate the branches.

---

<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:** [October 8, 2018, 5:39pm UTC](https://discourse.julialang.org/t/how-to-use-getproperty-setproperty/16033/3 "2018-10-08T17:39:20Z")

</div>

> [@e3c6](#):
>
> Or is the compiler able to eliminate the branching when it sees `a.b` (hence knows the actual symbol used)?

Yes, the compiler normally inlines and eliminates the branches when you call `getproperty` via `a.b`. (This is a [constant-propagation optimization](https://github.com/JuliaLang/julia/pull/24362) that was added in Julia 0.7.)

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 8, 2018, 6:12pm UTC](https://discourse.julialang.org/t/how-to-use-getproperty-setproperty/16033/4 "2018-10-08T18:12:11Z")

</div>

Two more questions.

1. Is there a macro to make adding properties easy? For example, something that automatically handles `propertynames`?
2. If someone writes `a.b = v` on a property that should not be set, what is the error that `setproperty!` should throw?

---

<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:** [October 8, 2018, 7:20pm UTC](https://discourse.julialang.org/t/how-to-use-getproperty-setproperty/16033/6 "2018-10-08T19:20:07Z")

</div>

> [@e3c6](#):
>
> - Is there a macro to make adding properties easy? For example, something that automatically handles `propertynames` ?

I don’t think there is any package for this yet.

Various packages have probably rolled their own utilities for this. e.g. in ZMQ.jl I wrote a little utility function to automatically generate the long sequence of `if … elseif … ` checks from a list of property symbols and corresponding functions to dispatch to for each property. See e.g. how the long list of `ZMQ.Socket` [properties is implemented](https://github.com/JuliaInterop/ZMQ.jl/blob/master/src/sockopts.jl#L119-L137).

> [@e3c6](#):
>
> - If someone writes `a.b = v` on a property that should not be set, what is the error that `setproperty!` should throw?

Something like `error(typeof(value), " has no writable field ", name)` seems common; as far as I can tell there is no special exception type for this.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [June 21, 2019, 8:20pm UTC](https://discourse.julialang.org/t/how-to-use-getproperty-setproperty/16033/7 "2019-06-21T20:20:39Z")

</div>

There is no issue with `getproperty` not being type stable?

---

<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:** [June 22, 2019, 3:00am UTC](https://discourse.julialang.org/t/how-to-use-getproperty-setproperty/16033/8 "2019-06-22T03:00:47Z")

</div>

> [@e3c6](#):
>
> There is no issue with `getproperty` not being type stable?

No, because of the constant propagation mentioned above: all of the branches except for the symbol you want are stripped out before inference.
