# Can we allow the syntax \`v.\[i\]\` to be converted to \`getindex.(v, i)\`?

**URL:** <https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414>\
**Category:** Internals & Design\
**Created:** [August 2, 2023, 6:38pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414 "2023-08-02T18:38:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [August 2, 2023, 6:38pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/1 "2023-08-02T18:38:23Z")

</div>

Hi!

I have one scenario that happens all the time. I get a vector of vectors and I need to obtain each component individually to perform some analyses and plot the data. Today, I do:

```julia
getindex.(v, 1)
getindex.(v, 2)
getindex.(v, 3)
...

```

However, I am wondering if we can allow the following syntax:

```julia
v.[1]
v.[2]
v.[3]
...

```

It makes sense to me because we are broadcasting the get index operation.

What do you think?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 2, 2023, 6:43pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/2 "2023-08-02T18:43:59Z")

</div>

see also [What to do about nonscalar indexing? · Issue #30845 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/30845)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 2, 2023, 7:29pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/3 "2023-08-02T19:29:42Z")

</div>

Bear in mind this is different from OP’s intention to broadcast over the array as well:

```julia
julia> A = zeros(10,2);

julia> broadcast((idxs...)->A[idxs...], 1:10, 1) # ~getindex.((A,), 1:10, 1)
10-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0

julia> getindex.(A, 1:10, 1)
ERROR: BoundsError
Stacktrace:
  [1] getindex
    @ ./number.jl:103 [inlined]

```

If I had to pick, I’d pick the former because it’s strange to think of a dotted instance as something that is iterated. I would prefer neither because it’s simpler to understand if we always dot the callable.

---

<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:** [August 2, 2023, 7:34pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/4 "2023-08-02T19:34:35Z")

</div>

TLDR: there are three conflicting meanings to what `A.[I, J]` could possibly mean:

- You are implicitly assuming `A` is a collection of arrays and you want to perform the same indexing operation over all of them (this would be `getindex.(A, (I,), (J,))`)
- You are implicitly assuming `A` is a single array and you want to broadcast over the passed indices — great for fusing these accesses or doing “diagonal” indexing (this would be `getindex.((A,), I, J)`)
- Or you (probably don’t) want both the array and the indices to participate in the broadcast simultaneously (this would be `getindex.(A, I, J)`)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 2, 2023, 8:39pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/5 "2023-08-02T20:39:56Z")

</div>

> [@mbauman](#):
>
> - You are implicitly assuming `A` is a collection of arrays and you want to perform the same indexing operation over all of them (this would be `getindex.(A, (I,), (J,))`)
> - You are implicitly assuming `A` is a single array and you want to broadcast over the passed indices — great for fusing these accesses or doing “diagonal” indexing (this would be `getindex.((A,), I, J)`)
> - Or you (probably don’t) want both the array and the indices to participate in the broadcast simultaneously (this would be `getindex.(A, I, J)`)

I get it, but like… does anyone look at that and not expect the second one? I understand you could interpret other ways, but assuming `A` is an array of arrays in the first and last case seems IMO contrarian and not really the way most people would read that syntax. Maybe someone will say differently, but I haven’t seen someone outside of that thread assume it would or could mean anything but `getindex.((A,), I, J)` like OP says. This is really a case of “perfect is the enemy of good” in my view.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 2, 2023, 8:41pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/6 "2023-08-02T20:41:07Z")

</div>

If you apply the rule we apply for every other function it would be number 3 though.

---

<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:** [August 2, 2023, 9:02pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/7 "2023-08-02T21:02:59Z")

</div>

> [@ChrisRackauckas](#):
>
> does anyone look at that and not expect the second one?

Yes, both me and OP. I often miss a generalization of `first, last` that work for any index, and have often wanted exactly what’s requested in OP.

Edit: after having read your post again, I’m not quite sure which one you’re arguing for. In any case, I tend to miss the same functionality as OP.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [August 2, 2023, 9:05pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/8 "2023-08-02T21:05:03Z")

</div>

> [@mbauman](#):
>
> Or you (probably don’t) want both the array and the indices to participate in the broadcast simultaneously (this would be `getindex.(A, I, J)`)

should be this one right? then the others can be disambiguated like `Ref(A).[I, J]`, `A.[Ref(I), Ref(J)]`, etc.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 2, 2023, 9:12pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/9 "2023-08-02T21:12:05Z")

</div>

> [@adienes](#):
>
> should be this one right? then the others can be disambiguated like `Ref(A).[I, J]`, `A.[Ref(I), Ref(J)]`, etc.

That looks reasonable enough.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [August 2, 2023, 9:34pm UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/10 "2023-08-02T21:34:00Z")

</div>

An alternative would be to `stack` it into a matrix and then just use regular indexing:

```julia
u = stack(v)
u[1, :]
u[2, :]
...

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [August 3, 2023, 11:03am UTC](https://discourse.julialang.org/t/can-we-allow-the-syntax-v-i-to-be-converted-to-getindex-v-i/102414/11 "2023-08-03T11:03:19Z")

</div>

Wrt developing multi indices access, as in `getindex.(Ref(x), is)`, further: also consider a more principled approach of [GitHub - andyferris/Indexing.jl: Generalized indexing for Julia](https://github.com/andyferris/Indexing.jl). There `Y = getindices(X, I)` always implies that `Y` has the same indices as `I` with values of `X`:

```julia
getindices(container, indices)

Return an indexable container with indices `keys(indices)` and values `container[i]` for
`i ∈ values(indices)`. This generalizes scalar `getindex(container, index)` for multiple
indices, for dictionaries, tuples, named tuples, etc.

```
