# What is empty index notation?

**URL:** <https://discourse.julialang.org/t/what-is-empty-index-notation/79676>\
**Category:** General Usage\
**Tags:** question, makie\
**Created:** [April 19, 2022, 8:51am UTC](https://discourse.julialang.org/t/what-is-empty-index-notation/79676 "2022-04-19T08:51:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Katamari](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@Katamari](https://discourse.julialang.org/u/Katamari)\
**Post date:** [April 19, 2022, 8:51am UTC](https://discourse.julialang.org/t/what-is-empty-index-notation/79676/1 "2022-04-19T08:51:34Z")

</div>

I saw the following notation in the documentation of Makie.

`x[] = 3.34`

[https://makie.juliaplots.org/stable/documentation/nodes/index.html#the\_observable\_structure](https://makie.juliaplots.org/stable/documentation/nodes/index.html#the_observable_structure)

What is this syntax?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [April 19, 2022, 8:54am UTC](https://discourse.julialang.org/t/what-is-empty-index-notation/79676/2 "2022-04-19T08:54:00Z")

</div>

You set the only element in `x` to `3.34`

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [April 19, 2022, 9:11am UTC](https://discourse.julialang.org/t/what-is-empty-index-notation/79676/3 "2022-04-19T09:11:15Z")

</div>

In this case it is:

```julia
julia> x = Observable(0.0)
Observable{Float64} with 0 listeners. Value:
0.0

julia> dump(x)
Observable{Float64}
  listeners: Array{Any}((0,))
  val: Float64 0.0
  inputs: #undef

julia> @which x[]
getindex(observable::Observable) in Observables at C:\Users\Oli\.julia\packages\Observables\OFj0u\src\Observables.jl:306

julia> @less x[]
Base.getindex(observable::Observable) = observable.val

```

It is setting the `val` part of the `Observable` type.  
Operator `[]` is called derefencing a `Ref` (reference) which you can find in the docs:  
[https://docs.julialang.org/en/v1.7/base/c/#Core.Ref](https://docs.julialang.org/en/v1.7/base/c/#Core.Ref)  
It is just a similar definition in Base:

```julia
julia> y=Ref(5)
Base.RefValue{Int64}(5)

julia> dump(y)
Base.RefValue{Int64}
  x: Int64 5

julia> @less y[]
getindex(b::RefValue) = b.x

```

---

<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:** [April 19, 2022, 10:15am UTC](https://discourse.julialang.org/t/what-is-empty-index-notation/79676/4 "2022-04-19T10:15:47Z")

</div>

You can also use this syntax for accessing the value of a zero-dimensional array. I would guess that this is where the syntax originated, N indices for N dimensions.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 20, 2022, 5:45am UTC](https://discourse.julialang.org/t/what-is-empty-index-notation/79676/5 "2022-04-20T05:45:19Z")

</div>

How can this be explained?

```julia
julia> x = [:a]
1-element Vector{Symbol}:
 :a

julia> x[]
:a

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [April 20, 2022, 6:10am UTC](https://discourse.julialang.org/t/what-is-empty-index-notation/79676/6 "2022-04-20T06:10:07Z")

</div>

The `a[]` syntax will return the only element in `a`.

```julia
julia> x = [:a]
1-element Vector{Symbol}:
 :a

julia> x[]
:a

julia> x = reshape([:a], Val(0))
0-dimensional Array{Symbol, 0}:
:a

julia> x[]
:a

julia> x = reshape([:a], Val(2))
1×1 Matrix{Symbol}:
 :a

julia> x[]
:a

```

This doesn’t work if `a` doesn’t have exactly one element

```julia
julia> x = [:a, :b]
2-element Vector{Symbol}:
 :a
 :b

julia> x[]
ERROR: BoundsError: attempt to access 2-element Vector{Symbol} at index []

julia> x = []
Any[]

julia> x[]
ERROR: BoundsError: attempt to access 0-element Vector{Any} at index []

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 20, 2022, 6:15am UTC](https://discourse.julialang.org/t/what-is-empty-index-notation/79676/7 "2022-04-20T06:15:01Z")

</div>

I mean why does it do that? It is inconsistent with @DNF’s guess

> [@DNF](#):
>
> I would guess that this is where the syntax originated, N indices for N dimensions.

Does Matlab do the same thing?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [April 20, 2022, 6:32am UTC](https://discourse.julialang.org/t/what-is-empty-index-notation/79676/8 "2022-04-20T06:32:48Z")

</div>

It does that because [indices may be omitted if all the trailing dimensions are of length `1`](https://docs.julialang.org/en/v1/manual/arrays/#Omitted-and-extra-indices). This implies that, for a 1-element `Array`, `x[]` is equivalent to `x[1]`, irrespective of how many dimensions the array has.

I am uncertain if Matlab does the same.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [April 20, 2022, 7:08am UTC](https://discourse.julialang.org/t/what-is-empty-index-notation/79676/9 "2022-04-20T07:08:19Z")

</div>

The way I think about it is as follows `x[]` is the same as `getindex(x)`. So if there is one-argument `getindex` defined for type of value `x` then `x[]` will work.
