# Choose between StaticArrays and regular arrays in a type stable way

**URL:** <https://discourse.julialang.org/t/choose-between-staticarrays-and-regular-arrays-in-a-type-stable-way/12407>\
**Category:** General Usage\
**Created:** [July 16, 2018, 7:28am UTC](https://discourse.julialang.org/t/choose-between-staticarrays-and-regular-arrays-in-a-type-stable-way/12407 "2018-07-16T07:28:33Z")\
**Posts on this page:** 3\
**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:** [July 16, 2018, 7:28am UTC](https://discourse.julialang.org/t/choose-between-staticarrays-and-regular-arrays-in-a-type-stable-way/12407/1 "2018-07-16T07:28:33Z")

</div>

For the StaticArrays aficionados here: is it possible to choose dynamically in a type stable way whether an operation is carried out with static arrays or with regular arrays?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 16, 2018, 8:33am UTC](https://discourse.julialang.org/t/choose-between-staticarrays-and-regular-arrays-in-a-type-stable-way/12407/2 "2018-07-16T08:33:49Z")

</div>

No, but as long as you have a function barrier to isolate the type instability, that should be fine.

So in pseudo code

```julia
struct MyProblem{T}
    myarray::T
end

function generate_problem(x::Int)
    if x > 100
        problem = MyProblem(DynamicArray)
   else
        problem = MyProblem(StaticArray)
   end
    do_computation(problem)
end

```

I think one of the important skills to learn with Julia is to be able to exploit that it is dynamic, while still getting the performance benefits of a static language by using function barriers.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [July 16, 2018, 8:45am UTC](https://discourse.julialang.org/t/choose-between-staticarrays-and-regular-arrays-in-a-type-stable-way/12407/3 "2018-07-16T08:45:58Z")

</div>

But if one of them can be the default output, then 2 variable names can be used and the result can be copied over at the end. I am assuming `do_computation` is expensive and returns the output as a `MyProblem` instance when given a static array wrapper but works inplace when passed normal array wrapper. I think this should be type stable if I understand correctly.

```julia
function generate_problem(x::Int)
   problem1 = MyProblem(DynamicArray)
   if x > 100
        do_computation(problem1)
   else
        problem2 = MyProblem(StaticArray)
        problem1.myarray .= do_computation(problem2).myarray
   end
   problem1
end

```
