# Type-stability with an elastic array in a struct

**URL:** <https://discourse.julialang.org/t/type-stability-with-an-elastic-array-in-a-struct/82298>\
**Category:** General Usage\
**Tags:** code\_warntype, type-stability, elasticarrays\
**Created:** [June 6, 2022, 9:59am UTC](https://discourse.julialang.org/t/type-stability-with-an-elastic-array-in-a-struct/82298 "2022-06-06T09:59:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://discourse.julialang.org/u/james3)\
**Post date:** [June 6, 2022, 9:59am UTC](https://discourse.julialang.org/t/type-stability-with-an-elastic-array-in-a-struct/82298/1 "2022-06-06T09:59:57Z")

</div>

I have defined a struct that contains an [elastic array](https://github.com/JuliaArrays/ElasticArrays.jl), similar to the following.

```julia
struct MyStruct
    myelas::ElasticMatrix{Int64}
end 

```

However, `@code_warntype` shows the following is not type-stable.

```julia
function test_struct(n)
    mystruct = MyStruct(ElasticMatrix{Int64}(undef, n, 0))
    append!(mystruct.myelas, ones(Int64, n))
    return mystruct
end

```

If I implement the same code here without the `struct` then all is okay.

```julia
function test(n)
    myelas = ElasticMatrix{Int64}(undef, n, 0)
    append!(myelas, ones(Int64, n))
    return myelas
end

```

How should I be defining `MyStruct` to achieve type-stability? Thanks.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [June 6, 2022, 10:40am UTC](https://discourse.julialang.org/t/type-stability-with-an-elastic-array-in-a-struct/82298/2 "2022-06-06T10:40:19Z")

</div>

Yes an elastic array has more parameters than that. In your case you could do

```julia
struct MyStruct
    myelas::typeof(ElasticMatrix{Int64}(undef, 0, 0))
end

```

to get the concrete type without delving into the internals of ElasticArrays.
