# Semicolon is sometimes significant inside square brackets

**URL:** <https://discourse.julialang.org/t/semicolon-is-sometimes-significant-inside-square-brackets/91950>\
**Category:** General Usage\
**Created:** [December 21, 2022, 12:30pm UTC](https://discourse.julialang.org/t/semicolon-is-sometimes-significant-inside-square-brackets/91950 "2022-12-21T12:30:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [December 21, 2022, 12:30pm UTC](https://discourse.julialang.org/t/semicolon-is-sometimes-significant-inside-square-brackets/91950/1 "2022-12-21T12:30:22Z")

</div>

Take the following example:

```julia
julia> [1]
1-element Vector{Int64}:
 1

julia> [1;]
1-element Vector{Int64}:
 1

```

In this case, there is no effect of putting a semicolon after the vector element. So what changes in the following case?

```julia
julia> [1:2]
1-element Vector{UnitRange{Int64}}:
 1:2

julia> [1:2;]
2-element Vector{Int64}:
 1
 2

```

Because I read semicolons as a way to effectivly end the line without an actual line break, making `[1:2;]` the same as

```julia
julia> [1:2
       ]
1-element Vector{UnitRange{Int64}}:
 1:2

```

, which is the same output as **_no_** semocolon, and different from the case _ **with** _ the semicolon. So how do semicolons actually influence code? Is it an effective newline in all cases but this one, or is there some more fundamental and consistent behaviour going on?

A sidequestion: is `[1:2;]` ever reccomended? It does the same as `collect(1:2)` in fewer characters, but it seems less clear and less widespread, so I am wondering about the place of this syntax in the Julia programming language.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 21, 2022, 1:11pm UTC](https://discourse.julialang.org/t/semicolon-is-sometimes-significant-inside-square-brackets/91950/2 "2022-12-21T13:11:54Z")

</div>

Instead of thinking of it as `collect`, it’s better to think of it as `vcat`.

You can vertically concatenate vectors with semicolons:

```julia
julia> [[1, 2]; [3, 4, 5]]
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

and you can also use it in matrix builder notation, using `;;` for horizontal concatenation:

```julia
julia> H = [1; 2;; 3; 4]
2×2 Matrix{Int64}:
 1 3
 2 4

julia> [0; [5,6];; [7 8]; H ]
3×3 Matrix{Int64}:
 0 7 8
 5 1 3
 6 2 4

```

where the semicolons are being used for vertical and horizontal concatenation. You can also space it out with newlines:

```julia
julia> [ 0 7 8
         [5,6] H ]
3×3 Matrix{Int64}:
 0 7 8
 5 1 3
 6 2 4

```

which is, again, consistent with the vector version:

```julia
julia> [1:2
        3:5]
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

However, it looks like the difference when there is only a single element is a special case. They’re parsed differently:

```julia
julia> :([1:2
       ]).head
:vect

julia> :([1:2;]).head
:vcat

```

I’m not entirely sure that this parsing rule is the best decision, but it’s what we have for now.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 21, 2022, 2:00pm UTC](https://discourse.julialang.org/t/semicolon-is-sometimes-significant-inside-square-brackets/91950/3 "2022-12-21T14:00:28Z")

</div>

> [@TheLateKronos](#):
>
> A sidequestion: is `[1:2;]` ever reccomended? It does the same as `collect(1:2)` in fewer characters, but it seems less clear and less widespread, so I am wondering about the place of this syntax in the Julia programming language.

I would write `collect(1:2)` for clarity and use `[1:2;]` only in instruction material to make a point about the syntax rules, except in special cases for example when it resonates with neighboring code which makes the whole thing easier to read. Example:

```julia
julia> a = 1:2
1:2

julia> [a;]
2-element Vector{Int64}:
 1
 2

julia> [a;;]
2×1 Matrix{Int64}:
 1
 2

julia> [a;;;]
2×1×1 Array{Int64, 3}:
[:, :, 1] =
 1
 2

```

If I need these three operations for some reason, I would probably write `[a;]` for “symmetry”.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 28, 2022, 1:38pm UTC](https://discourse.julialang.org/t/semicolon-is-sometimes-significant-inside-square-brackets/91950/4 "2022-12-28T13:38:52Z")

</div>

I just stumbled upon the following use of `;`. This one I would strictly reserve to code golf and obfuscation contests 🙂

```julia
a = [[1,2],[3]]
[a...;]

```

---

<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:** [December 28, 2022, 2:07pm UTC](https://discourse.julialang.org/t/semicolon-is-sometimes-significant-inside-square-brackets/91950/5 "2022-12-28T14:07:03Z")

</div>

> [@uniment](#):
>
> it’s better to think of it as `vcat`.

Unless I’m mistaken, `[x;]` is not just _like_ `vcat`, but is the same thing, ie, the former is syntax sugar for the latter.
