# Brevity and StaticArrays

**URL:** <https://discourse.julialang.org/t/brevity-and-staticarrays/6142>\
**Category:** Internals & Design\
**Created:** [September 28, 2017, 5:03pm UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142 "2017-09-28T17:03:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [September 28, 2017, 5:03pm UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/1 "2017-09-28T17:03:06Z")

</div>

I like StaticArrays – the ability to say, “The _type_ of this variable is a matrix with such-and-such a size.” I also love how powerfully brief my code can be in Julia, allowing me to stay very “mathy”, using LaTeX characters, like `\times` for cross product.

Call it vain, but I wish there were a briefer way to create a StaticArray, like regular old brackets. Writing out the whole word `SVector` interferes with the flow. I like how I can specify an IPv4, for instance, as `ip"127.0.0.1"`. Could something like this work for StaticArrays, but with brackets? Like:

```julia
x = s[1.; 2.; 3.] # It's an SVector!

```

Or maybe we could use some other type of bracket or something.

Does this make sense? Does Julia support creations like this, or would Julia have to change to facilitate it? Or is there already a brief way to specify StaticArrays that I don’t see in the StaticArrays doc?

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [September 28, 2017, 5:44pm UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/2 "2017-09-28T17:44:48Z")

</div>

This [PR](https://github.com/JuliaLang/julia/pull/23547) would let you do `@s[1.;2.;3.]`.

---

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [September 28, 2017, 6:14pm UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/3 "2017-09-28T18:14:29Z")

</div>

Thanks. That’s an interesting PR, with a similar motivation to my own. I’m not sure writing a macro for every array construction feels right to me though. In this case, I think a type alias is clearer.

Macro method (assuming that PR goes in):

```julia
x = @s[1.; 2.; 3.] # where @s was defined somewhere to use @SVector

```

Type alias:

```julia
x = s(1., 2., 3.) # where s = SVector was defined somewhere

```

Regular:

```julia
x = SVector(1., 2., 3.)

```

But I dearly wish I could use square brackets instead.

Proposed:

```julia
x = s[1.; 2.; 3]

```

This would require that the `s[...]` pattern could be parsed.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 28, 2017, 6:51pm UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/4 "2017-09-28T18:51:24Z")

</div>

The problem is that `s[1, 2, 3]` (or, equivalently, `s[1; 2; 3]`) is already parsed and has a well-defined meaning: it gives you a vector with element type `s` initialized to `[1, 2, 3]`. So we have

```julia
julia> Float64[1, 2, 3]
3-element Array{Float64,1}:
 1.0
 2.0
 3.0

```

Thus, if you have `s = SVector{3, Float64}`, and you try to do `s[1, 2, 3]`, you’re asking for a vector of SVectors (this fails because you can’t convert an integer to an SVector).

So the closest you can get is with that PR mentioned above and something like `@s[1, 2, 3]`, since the macro can insert the appropriate syntax to do what you want.

---

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [September 28, 2017, 7:30pm UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/5 "2017-09-28T19:30:22Z")

</div>

Thanks @rdeits. I never use that syntax and had forgetten about it. You’re right that that’s devastating to my proposal. Do you have any alternate proposal? Different brackets? Some other clever idea?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 28, 2017, 7:39pm UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/6 "2017-09-28T19:39:18Z")

</div>

Personally I would go for `SVector(1, 2, 3)` or

```julia
svec(x...) = SVector(x...)
svec(1, 2, 3)

```

I realize it’s not as concise as `[1, 2, 3]` but I’ve found that I personally don’t mind it too much.

One other thing you could do if you _really_ want is create a macro that turns all instances of `s[1, 2, 3]` into `SVector(1, 2, 3)` within a particular block of code. That will give you the brevity you’re looking for, at the cost of making your code harder for others to understand.

---

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [September 28, 2017, 7:41pm UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/7 "2017-09-28T19:41:51Z")

</div>

I had thought about this a lot.  
The typed array gets it behavior from the definitions

```julia
getindex(::Type{T}, vals...) = .......
typed_hcat(::Type{T}, vals...) = .......
typed_hvcat(::Type{T}, vals...) = .......

```

I thought that maybe one could define something like

```julia
getindex(::Type{Type{T}}, vals...) where T<:StaticArray = ......
typed_hcat(::Type{Type{T}}, vals...) where T<:StaticArray = ......
typed_hvcat(::Type{Type{T}}, vals...) where T<:StaticArray = ......
const S = Type{SArray}

```

and use as

```julia
S[1, 2, 3]
S[1 2 3]
S[1 2; 3 4]

```

But with this I find some drawbacks in the generated native code, especially in the `typed_hvcat` case.  
Anyway, I guess those code generations are fixable.

---

<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:** [September 28, 2017, 7:47pm UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/8 "2017-09-28T19:47:47Z")

</div>

I’d love to see something like this work:

```julia
⟦1, 2, 3⟧
⟦1, 2; 3, 4⟧

```

---

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [September 28, 2017, 8:04pm UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/9 "2017-09-28T20:04:12Z")

</div>

> [@rdeits](#):
>
> Personally I would go for SVector(1, 2, 3)

There’s also the issue of having a concise way to write literal static matrices…

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [October 3, 2017, 4:20am UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/10 "2017-10-03T04:20:45Z")

</div>

So people don’t like something like `@SA[1, 2, 3]`? I would have guessed this is short enough.

We can do `sa[1, 2, 3]` if `sa` is not a type, but say some singleton instance created for just this case.

I’ve been thinking more generally… it would be nice to have some more generalised “container literal” syntax like `@DF[a = [1, 2, 3], b = [4, 5, 6]]` for a `DataFrame`, or whatever. Apart from macros I’m not sure how to proceed…

---

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [October 3, 2017, 10:00am UTC](https://discourse.julialang.org/t/brevity-and-staticarrays/6142/11 "2017-10-03T10:00:18Z")

</div>

After thinking about all those options I started to really like `@stuff[...]`, thas why I went for the [PR](https://github.com/JuliaLang/julia/pull/23547).
