Are fields with structtypes to be avoided as well?

In the manual it is recommended to avoid abstract type fields: 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> 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> 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 ?

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> 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.

2 Likes

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

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.