# What does \[\] mean in this case?

**URL:** <https://discourse.julialang.org/t/what-does-mean-in-this-case/43175>\
**Category:** General Usage\
**Created:** [July 16, 2020, 12:40pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175 "2020-07-16T12:40:08Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [July 16, 2020, 12:40pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/1 "2020-07-16T12:40:08Z")

</div>

I’m trying to figure out a functionality of Makie.jl, which I can’t find in documentation. In the process, I noticed a syntax I don’t understand:

```julia
julia> s = Scene()

julia> fieldnames(Scene)
(:parent, :events, :px_area, :clear, :camera, :camera_controls, :data_limits, :transformation, :plots, :theme, :attributes, :children, :current_screens, :updated)

julia> typeof(s.update_limits[])
Bool

```

What does `update_limits[]` mean syntactically? It is not a field of `Scene` `struct` and I can’t find this use of `[]` in the documentation.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [July 16, 2020, 12:45pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/2 "2020-07-16T12:45:09Z")

</div>

Is `update_limits` a `Ref`?

```julia
julia> x = Ref{Int}()
Base.RefValue{Int64}(0)

julia> x[] = 1
1

julia> x
Base.RefValue{Int64}(1)

julia> x[]
1

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 16, 2020, 12:52pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/3 "2020-07-16T12:52:52Z")

</div>

`getindex` without the indices is the generic syntax to access the only element of a single-element container, a `Ref` but also a plain `Array`:

```julia
julia> x = [1]
1-element Array{Int64,1}:
 1

julia> x[]
1

julia> getindex(x)
1

```

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [July 16, 2020, 12:53pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/4 "2020-07-16T12:53:56Z")

</div>

> [@johnmyleswhite](#):
>
> Is `update_limits` a `Ref` ?

I don’t know:

```julia
julia> typeof(s.update_limits)
Observable{Any}

```

Thank you for pointing me towards references. The search engine for Julia manual is helpless with `"[]"` or `[]` search queries. I get zero or 152 unrelated results.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 16, 2020, 12:56pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/5 "2020-07-16T12:56:48Z")

</div>

> [@pauljurczak](#):
>
> ```julia
> julia> typeof(s.update_limits)
> Observable{Any}
> 
> ```

I assume this is from [`Observables.jl`](https://github.com/JuliaGizmos/Observables.jl)? If so, quoting from the [documentation](https://juliagizmos.github.io/Observables.jl/latest/#Observables-1):

> [@](#):
>
> Observables are like `Ref` s but you can listen to changes.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 16, 2020, 1:01pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/6 "2020-07-16T13:01:55Z")

</div>

Nice @giordano, learned something new today 🙂

Is it correct to say that `a[]` is also better than `a[1]` because it is converted to `a[begin]`? So it uses the correct `firstindex` for the collection?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 16, 2020, 1:04pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/7 "2020-07-16T13:04:52Z")

</div>

I wouldn’t encourage using `a[]` for arrays, `a[begin]` is more explicit and clear, albeit a bit verbose (and I think requires Julia v1.something, with something != 0). But for single-element arrays maybe it’s fine anyway 🤔

**Edit** : the problem of `[]` is that it breaks with containers with more than one element:

```julia
julia> x = [1, 2]
2-element Array{Int64,1}:
 1
 2

julia> x[]
ERROR: BoundsError: attempt to access 2-element Array{Int64,1} at index []

```

So you _must_ be sure that the container has a single element in order to use it. For `Ref`-like containers this is usually the case (they have by design only one element, just make sure the object is always a `Ref`), but for arrays that can grow this assumption may be wrong.

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [July 16, 2020, 1:23pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/8 "2020-07-16T13:23:15Z")

</div>

How does

```julia
s = S()
s.aname[]

```

work when `aname` is not a field name of type `S`?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 16, 2020, 1:34pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/9 "2020-07-16T13:34:52Z")

</div>

> [@giordano](#):
>
> **Edit** : the problem of `[]` is that it breaks with containers with more than one element:

That’s a feature! It’s wholly consistent with how [omitting indexing dimensions](https://docs.julialang.org/en/v1/manual/arrays/#Omitted-and-extra-indices-1) work in general. In other words, it’s a nice shorthand for grabbing the only element — and ensuring that there’s only one.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 16, 2020, 1:35pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/10 "2020-07-16T13:35:50Z")

</div>

I didn’t want to imply that’s a bug, only point out that if you expect to work with any container (of any size!) you can have problems 🙂

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [July 16, 2020, 1:46pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/11 "2020-07-16T13:46:58Z")

</div>

> [@mbauman](#):
>
> It’s wholly consistent with how [omitting indexing dimensions](https://docs.julialang.org/en/v1/manual/arrays/#Omitted-and-extra-indices-1) work in general

It would help if this statement in the manual:

_When omitting all indices with A[], this semantic provides a simple idiom to retrieve the only element in an array and simultaneously ensure that there was only one element._

was searchable with `[]` query.

---

<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:** [July 16, 2020, 1:56pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/12 "2020-07-16T13:56:36Z")

</div>

> [@mbauman](#):
>
> In other words, it’s a nice shorthand for grabbing the only element — and ensuring that there’s only one.

While technically it works, I find it somewhat obscure to rely on `[]` doing this.

I would suggest `only` instead, which was explicitly introduced in 1.4 for this.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 16, 2020, 3:41pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/13 "2020-07-16T15:41:09Z")

</div>

Nice to learn about `only` as well, though I think that `[]` may be nicer in some contexts like many observables lifted in a Makie.jl visualization for example. Placing `only` everywhere can pollute the code.

---

<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:** [July 16, 2020, 3:43pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/14 "2020-07-16T15:43:11Z")

</div>

> [@juliohm](#):
>
> though I think that `[]` may be nicer in some contexts like many observables lifted in a Makie.jl visualization for example.

Observables are guaranteed to only have one item and using `[]` is perfectly natural for them. `only` is to be used on e.g. a `Vector` where you expect it to only have one item after e.g. some filtering step.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 16, 2020, 4:34pm UTC](https://discourse.julialang.org/t/what-does-mean-in-this-case/43175/15 "2020-07-16T16:34:09Z")

</div>

> [@giordano](#):
>
> **Edit** : the problem of `[]` is that it breaks with containers with more than one element:

I would only use `[]` on “zero-dimensional” containers (e.g. `Ref`, zero-dimensional arrays, `Observable`, and similar objects), where passing an index doesn’t make sense and they are guaranteed to contain exactly one element.
