# Are fields with structtypes to be avoided as well?

**URL:** <https://discourse.julialang.org/t/are-fields-with-structtypes-to-be-avoided-as-well/34515>\
**Category:** Performance\
**Created:** [February 12, 2020, 1:53pm UTC](https://discourse.julialang.org/t/are-fields-with-structtypes-to-be-avoided-as-well/34515 "2020-02-12T13:53:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [February 12, 2020, 1:53pm UTC](https://discourse.julialang.org/t/are-fields-with-structtypes-to-be-avoided-as-well/34515/1 "2020-02-12T13:53:00Z")

</div>

In the manual it is recommended to avoid abstract type fields: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-fields-with-abstract-type-1).  
It is not precised if this is also the case for struct type. Example:

```julia
julia> struct Foo{T} x::T end
julia> isabstracttype(Foo), isconcretetype(Foo), isstructtype(Foo)
(false, false, true)
julia> struct Bar foo::Foo end

```

Bar is not fully parametrized like this. A Bar struct could have different memory representations depending on the type of foo.x. Am I correct to say that I should do either of the following ?

```julia
julia> struct Bar{T} foo::Foo{T} end
julia> struct Bar{T <: Foo} foo::T end

```

If yes, should it not be specified in the manual ?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [February 12, 2020, 2:01pm UTC](https://discourse.julialang.org/t/are-fields-with-structtypes-to-be-avoided-as-well/34515/2 "2020-02-12T14:01:13Z")

</div>

> [@HenriDeh](#):
>
> Am I correct to say that I should do either of the following ?

Yes.  
Not only can the memory representations be different, but consider any function method `buz(bar::Bar)` that uses `bar.foo.x`. If it is going to be type stable, it has to be able to infer the type of `bar.foo.x`.  
Doing that with this definition:

```julia
julia> struct Bar foo::Foo end

```

is impossible, because `x` could be anything. But using either of the `Bar{T}` definitions will make the type inferrable.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 12, 2020, 2:21pm UTC](https://discourse.julialang.org/t/are-fields-with-structtypes-to-be-avoided-as-well/34515/3 "2020-02-12T14:21:08Z")

</div>

> [@HenriDeh](#):
>
> struct Bar{T \<: Foo} foo::T end

This one is better as you end up with a simpler relationship between type parameters and fields.

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [February 12, 2020, 3:11pm UTC](https://discourse.julialang.org/t/are-fields-with-structtypes-to-be-avoided-as-well/34515/4 "2020-02-12T15:11:01Z")

</div>

Okay thank you. I think this should be specified in the julia documentation in the performance tips. Because it says abstract types when it should be non-concrete types.
