Why is Julia allocating when accessing these structs?

Array{T} is not a concrete type. Array{T,1} (or equivalently Vector{T}) is. You are missing this type parameter, so the field is abstractly typed.

In your second, parametric version you almost get there except you write

If you’d instead written return AllocationsStruct(subStructArr), the type would have been correctly inferred.

But the preceding comment about type stability after s = 0 also holds.

5 Likes