# Collection of objects with a trait

**URL:** <https://discourse.julialang.org/t/collection-of-objects-with-a-trait/33863>\
**Category:** General Usage\
**Tags:** traits\
**Created:** [January 27, 2020, 5:53pm UTC](https://discourse.julialang.org/t/collection-of-objects-with-a-trait/33863 "2020-01-27T17:53:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [January 27, 2020, 5:53pm UTC](https://discourse.julialang.org/t/collection-of-objects-with-a-trait/33863/1 "2020-01-27T17:53:24Z")

</div>

Has anyone given some thought how to work with collections of objects that have a (set of) trait(s)? How to create, maintain, iterate… What would a `Base.iterate` extension look like?

---

<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:** [January 28, 2020, 5:53am UTC](https://discourse.julialang.org/t/collection-of-objects-with-a-trait/33863/2 "2020-01-28T05:53:56Z")

</div>

An example of something concrete would help clarify this question.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [January 28, 2020, 4:32pm UTC](https://discourse.julialang.org/t/collection-of-objects-with-a-trait/33863/3 "2020-01-28T16:32:53Z")

</div>

Well, I think one of the points of having traits is that the compiler can figure out types at compile time. However, that seems to me much more difficult when objects possessing a given trait are elements of an array. The array must be heterogeneous (I think), which makes dispatch dynamic. Am I wrong?

---

<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:** [January 28, 2020, 4:46pm UTC](https://discourse.julialang.org/t/collection-of-objects-with-a-trait/33863/4 "2020-01-28T16:46:05Z")

</div>

Sure, arrays without concrete element types will be suboptimal generally for compile-time dispatch, including traits.

I was asking about a concrete problem to see if there is a better way to formulate a solution (cf [XY problem](https://en.wikipedia.org/wiki/XY_problem)).

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [January 28, 2020, 4:48pm UTC](https://discourse.julialang.org/t/collection-of-objects-with-a-trait/33863/5 "2020-01-28T16:48:26Z")

</div>

Right you are. 🙂

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 28, 2020, 5:24pm UTC](https://discourse.julialang.org/t/collection-of-objects-with-a-trait/33863/6 "2020-01-28T17:24:14Z")

</div>

I wonder if [https://github.com/rdeits/ConcreteInterfaces.jl](https://github.com/rdeits/ConcreteInterfaces.jl) might be helpful? FWIW I’ve never actually _used_ it in a serious project, but I keep thinking it might be helpful.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [January 28, 2020, 6:12pm UTC](https://discourse.julialang.org/t/collection-of-objects-with-a-trait/33863/7 "2020-01-28T18:12:56Z")

</div>

Nice! That seems to be precisely what I was asking about. Thanks.
