# Constructing SVector with a loop

**URL:** <https://discourse.julialang.org/t/constructing-svector-with-a-loop/15372>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 23, 2018, 11:00am UTC](https://discourse.julialang.org/t/constructing-svector-with-a-loop/15372 "2018-09-23T11:00:16Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [September 23, 2018, 3:32pm UTC](https://discourse.julialang.org/t/constructing-svector-with-a-loop/15372/7 "2018-09-23T15:32:13Z")

</div>

[`Setfield.@set`](https://jw3126.github.io/Setfield.jl/stable/#Setfield.@set-Tuple%7BAny%7D) is a great tool for manipulating `StaticArrays` efficiently

```julia
using Setfield
using StaticArrays

function SFib1(::Val{S}) where S
    values = zeros(SVector{S, Int})
    @inbounds for i in 1:S
        values = @set values[i] = (i == 1 || i == 2) ? 1 : values[i-1] + values[i-2]
    end
    values
end

```

```llvm
julia> @code_llvm SFib1(Val(4))

; Function SFib1
; Location: REPL[132]:2
define void @julia_SFib1_38655({ [4 x i64] }* noalias nocapture sret) {
L45.3:
; Location: REPL[132]:4
; Function macro expansion; {
; Location: /home/takafumi/.julia/packages/Setfield/X9IQb/src/sugar.jl:77
  %1 = bitcast { [4 x i64] }* %0 to <4 x i64>*
  store <4 x i64> <i64 1, i64 1, i64 2, i64 3>, <4 x i64>* %1, align 8
  ret void
;}
}

```

---

_[View the full topic](https://discourse.julialang.org/t/constructing-svector-with-a-loop/15372)._
