# Help with converting array to Static Array

**URL:** https://discourse.julialang.org/t/help-with-converting-array-to-static-array/23861
**Category:** New to Julia
**Tags:** convert, staticarrays, constructors
**Created:** [May 5, 2019, 5:24am UTC](https://discourse.julialang.org/t/help-with-converting-array-to-static-array/23861 "2019-05-05T05:24:34Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [May 5, 2019, 5:24am UTC](https://discourse.julialang.org/t/help-with-converting-array-to-static-array/23861/1 "2019-05-05T05:24:34Z")

</div>

I wish to speed up my datastructure by converting array type to Static Array but I encounter an error. What did I do wrong?

```julia
using StaticArrays

datetype = Array{Int64,1}
datestatictype = SVector{3,Int64}

function printdate(x::datetype)
    println("year ",x[1]," month ",x[2]," day ",x[3])
end

function printdatestatic(x::datestatictype)
    println("year ",x[1]," month ",x[2]," day ",x[3])
end

a = Int64[2019,5,6]
printdate(a)

b = SVector{3,Int64}[2019,5,6]
printdatestatic(b)

```

with output

```julia
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.1.0 (2019-01-21)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

year 2019 month 5 day 6
ERROR: LoadError: MethodError: Cannot `convert` an object of type Int64 to an object of type SArray{Tuple{3},Int64,1,3}
Closest candidates are:
  convert(::Type{SA<:StaticArray}, ::SA<:StaticArray) where SA<:StaticArray at /Users/ssiew/.julia/packages/StaticArrays/VyRz3/src/convert.jl:11
  convert(::Type{SA<:StaticArray}, ::StaticArray) where SA<:StaticArray at /Users/ssiew/.julia/packages/StaticArrays/VyRz3/src/convert.jl:10
  convert(::Type{SA<:StaticArray}, ::AbstractArray) where SA<:StaticArray at /Users/ssiew/.julia/packages/StaticArrays/VyRz3/src/convert.jl:22
  ...
Stacktrace:
 [1] setindex!(::Array{SArray{Tuple{3},Int64,1,3},1}, ::Int64, ::Int64) at ./array.jl:767
 [2] getindex(::Type{SArray{Tuple{3},Int64,1,3}}, ::Int64, ::Int64, ::Int64) at ./array.jl:353
 [3] top-level scope at none:0
in expression starting at /Users/ssiew/juliascript/StaticArrayTest.jl:17

```

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [May 5, 2019, 6:03am UTC](https://discourse.julialang.org/t/help-with-converting-array-to-static-array/23861/2 "2019-05-05T06:03:02Z")

</div>

You missed the parenthesis `b = SVector{3,Int64}([2019,5,6])`. Without that, it tries to construct an array of Svectors so the error.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [May 5, 2019, 6:33am UTC](https://discourse.julialang.org/t/help-with-converting-array-to-static-array/23861/3 "2019-05-05T06:33:50Z")

</div>

Note that you can also do

```julia
julia> b = @SVector [2019,5,6]
3-element SArray{Tuple{3},Int64,1,3} with indices SOneTo(3):
 2019
    5
    6

julia> @macroexpand @SVector [2019,5,6]
:((SArray{Tuple{3},T,1,3} where T)((2019, 5, 6)))

```

This macro avoids creating any temporaries and is type stable. Therefore it should be fast on top of convenient.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 5, 2019, 7:33am UTC](https://discourse.julialang.org/t/help-with-converting-array-to-static-array/23861/4 "2019-05-05T07:33:45Z")

</div>

> [@tomaklutfu](#):
>
> ```julia
> b = SVector{3,Int64}([2019,5,6])
> 
> ```

Don’t use `[]` here. Then you are making a vector and then converting it. Write

```julia
SVector{3}(2019, 5, 6)

```

or use the macro.

Edit: Actually, simply `SVector(2019, 5, 6)` will do.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 5, 2019, 8:25am UTC](https://discourse.julialang.org/t/help-with-converting-array-to-static-array/23861/5 "2019-05-05T08:25:17Z")

</div>

> [@DNF](#):
>
> Don’t use `[]` here. Then you are making a vector and then converting it.

You are of course right given the context, but all of the constructors

```julia
SVector{N,T}(vec)
SVector{T}(args...)
SVector(args...)
@SVector [args...]

```

are useful depending on the format of the input arguments, and whether the type is known or is meant to be figured out using promotion, and whether `N` is meant to be enforced/checked.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 5, 2019, 9:07am UTC](https://discourse.julialang.org/t/help-with-converting-array-to-static-array/23861/6 "2019-05-05T09:07:57Z")

</div>

Agreed, but since the poster was uncertain how to create `SVector`s in general, I considered it useful to mention the most obvious constructor.
