# Static Array type syntax

**URL:** https://discourse.julialang.org/t/static-array-type-syntax/21927
**Category:** General Usage
**Created:** [March 15, 2019, 7:52pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927 "2019-03-15T19:52:53Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_Ellison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_ellison/32/5749_2.png) [@Alex\_Ellison](https://discourse.julialang.org/u/Alex_Ellison)
#### Post date: [March 15, 2019, 7:52pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/1 "2019-03-15T19:52:53Z")

</div>

If we look at the documentation for StaticArrays we see this:

```julia
SArray{S}(a::Array)

```

**Construct a statically-sized array of dimensions `S` (expressed as a `Tuple{...}` ) using the data from `a` . The `S` parameter is mandatory since the size of `a` is unknown to the compiler (the element type may optionally also be specified).**  
[https://juliaarrays.github.io/StaticArrays.jl/stable/pages/api.html#StaticArrays.SArray](https://juliaarrays.github.io/StaticArrays.jl/stable/pages/api.html#StaticArrays.SArray)

That seems pretty straightforward, yet when I try to use this it fails:

```julia
julia> m = zeros(3,3,3)
# output omitted
julia> SArray{size(m)}(m)
ERROR: TypeError: in SArray, in S, expected S<:Tuple, got Tuple{Int64,Int64,Int64}
Stacktrace:
 [1] top-level scope at none:0

julia> isa(size(m), Tuple)
true

```

Julia seems mad at me for not giving it a tuple conveying the size as the manual instructs, however we can plainly see the `size` I pass in is a tuple. Where is my logic breaking down?  
Thanks

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [March 15, 2019, 8:17pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/2 "2019-03-15T20:17:22Z")

</div>

size(m) is a runtime call to a function. The compiler must know the size at compile time, ie, you have to enter it as a literal

---

<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: [March 15, 2019, 8:24pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/3 "2019-03-15T20:24:45Z")

</div>

It should be a tuple type, I think e.g.

```julia
julia> Tuple{3,3,3}
Tuple{3,3,3}

```

At least that is what the error message is saying.

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [March 15, 2019, 8:50pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/4 "2019-03-15T20:50:41Z")

</div>

The use is

```julia
julia> using StaticArrays
julia> m = zeros(3,3,3);
julia> s=SArray{Tuple{size(m)...}}(m)
3×3×3 SArray{Tuple{3,3,3},Float64,3,27}:
...

```

However, this is pretty slow and not for use in loops:

```julia
julia> using BenchmarkTools
julia> g(m)=SArray{Tuple{size(m)...}}(m);
julia> @btime g($m);
  1.998 μs (15 allocations: 1.09 KiB)
julia> @btime SArray{$(Tuple{size(m)...})}($m);
  6.554 ns (0 allocations: 0 bytes)

```

It is however a valid and very julian optimization to construct the type describing the desired size outside of a long computation (via e.g. `sz = Tuple{size(m)...}`), and then have a function boundary into the long computation (e.g. `expensive_stuff(m, other_args, ::Type{sz}) where sz`). This way your long and expensive computation gets compiled for every size you pass in.

If you know the sizes at coding-time (e.g. you know that you want 3x3x3 tensors because 3d elasticity, or something) then you can of course just plug them in.

(actually, I think 2us is pretty fast for what we ask julia to do here)

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [March 15, 2019, 11:52pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/5 "2019-03-15T23:52:56Z")

</div>

It may be better to use the `@SArray` macro instead. You don’t have to allocate a normal array first.

```julia
julia> @SArray zeros(3,3,3)
3×3×3 SArray{Tuple{3,3,3},Float64,3,27}:
[:, :, 1] =
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0

[:, :, 2] =
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0

[:, :, 3] =
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0

```

---

<div class="post-metadata">

### Author: ![Alex\_Ellison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_ellison/32/5749_2.png) [@Alex\_Ellison](https://discourse.julialang.org/u/Alex_Ellison)
#### Post date: [March 16, 2019, 10:31pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/6 "2019-03-16T22:31:54Z")

</div>

Ok I did not expect to learn as many things as I did from this post, cool, guys. I think the biggest one was the difference between `Tuple{3,3,3}` and `(3,3,3)`… understanding that was enough to unstick me, thanks all.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [March 17, 2019, 10:42am UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/7 "2019-03-17T10:42:48Z")

</div>

> [@Alex\_Ellison](#):
>
> the difference between `Tuple{3,3,3}` and `(3,3,3)...`

So what is the difference? The second expression is also a tuple. Don’'t get it. 🤔

---

<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: [March 17, 2019, 12:02pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/8 "2019-03-17T12:02:09Z")

</div>

The first is a type, the second is a value:

```julia
julia> typeof(Tuple{3,3,3})
DataType

julia> typeof((3,3,3))
Tuple{Int64,Int64,Int64}

```

Also, the first one is a pretty interesting parametric type: there could be no value `x::Tuple{3,3,3}`, but the language does not check type parameters so it’s OK, just like

```julia
struct Foo{T}
    x::T
end

Foo{3}

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [March 17, 2019, 5:47pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/9 "2019-03-17T17:47:24Z")

</div>

Ok, but what is the difference between  
Tuple{3,3,3}

and  
Tuple{Int64,Int64,Int64} ?

---

<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: [March 17, 2019, 6:53pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/10 "2019-03-17T18:53:51Z")

</div>

One of the tuple types contain `3` and the other `Int`. I feel this is a trick question?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [March 17, 2019, 7:18pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/11 "2019-03-17T19:18:32Z")

</div>

Consider:

```julia
julia> (3,3,3) isa Tuple{Int,Int,Int}
true

julia> (3,3,3) isa Tuple{3,3,3}
false

```

indeed there exists no value which `isa Tuple{3,3,3}`.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [March 17, 2019, 7:50pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/12 "2019-03-17T19:50:56Z")

</div>

But why is the expression Tuple{3,3,3} is then used in the following context, if no variable of this type can exist?

> [@foobar\_lv2](#):
>
> ```julia
> julia> m = zeros(3,3,3);
> julia> s=SArray{Tuple{size(m)...}}(m) 
> 3×3×3 SArray{Tuple{3,3,3},Float64,3,27}:
> 
> ```

---

<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: [March 17, 2019, 7:55pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/13 "2019-03-17T19:55:55Z")

</div>

Same reason as you can have a type

```julia
struct Foo{N} end

```

and create a `Foo{2}` even though there is no variable that can have the type `2`.

---

<div class="post-metadata">

### Author: ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)
#### Post date: [March 17, 2019, 8:18pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/14 "2019-03-17T20:18:14Z")

</div>

The compiler gets to see the types, but not the values of variables. So this is in a sense just a trick to pass more information to the compiler.

For a normal `Array{Int64,N}` all it knows is the element type and the number of dimensions; the point of StaticArrays is that it also knows there will be exactly 3x3x3 elements here, allowing optimisations which make sense for 3 but not for a million.

---

<div class="post-metadata">

### Author: ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)
#### Post date: [March 24, 2025, 11:35am UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/15 "2025-03-24T11:35:33Z")

</div>

Blast from the past! Ancient thread revived.

… which is a great remark, improbable22, because it explains the reason why this puzzling parametrization of SArray was chosen in the first place.

The thread is from 2019, I write in 2025 (Julia 1.11). Since then Tuples can be type parameters: hypothetically, could SArray be redesigned so that `SArray{(2,2,2),Float64}` does what we expect? “Hypothetically” because this would be a breaking change.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [March 24, 2025, 2:01pm UTC](https://discourse.julialang.org/t/static-array-type-syntax/21927/16 "2025-03-24T14:01:53Z")

</div>

Thank you for your contribution!  
However, we usually refrain from ressuscitating threads that are half a decade old, because it pings everyone in the process. It is usually better to start a new thread and link to the old one 🙂
