# Neat way of broadcasting getindex and getfield

**URL:** https://discourse.julialang.org/t/neat-way-of-broadcasting-getindex-and-getfield/28706
**Category:** General Usage
**Created:** [September 13, 2019, 1:12am UTC](https://discourse.julialang.org/t/neat-way-of-broadcasting-getindex-and-getfield/28706 "2019-09-13T01:12:01Z")
**Posts on this page:** 5
**Page:** 1

<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: [September 13, 2019, 1:12am UTC](https://discourse.julialang.org/t/neat-way-of-broadcasting-getindex-and-getfield/28706/1 "2019-09-13T01:12:01Z")

</div>

I often find myself wanting to broadcast `getindex` and `getfield`, which is perfectly doable, but could enjoy a nicer syntax. I figured out if I define the operator `..` in the right way, I get a kind-of nice syntax for doing this without macros:

```julia
a = [randn(3) for _ in 1:4];
b = [complex(i,i+1) for i in 1:4];
(..)(x::AbstractArray,i...) = getindex.(x,i...)
(..)(x::AbstractArray,i::Symbol) = getfield.(x,i)

julia> a..2 == getindex.(a, 2)
true

julia> b..:re == getfield.(b, :re)
true

```

Edit: To broadcast getindex for dicts, the following method is useful

```julia
(..)(x::AbstractDict,i...) = getindex.(Ref(x), i...)
mydict..mykeys # now works

```

---

<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: [September 13, 2019, 2:07am UTC](https://discourse.julialang.org/t/neat-way-of-broadcasting-getindex-and-getfield/28706/2 "2019-09-13T02:07:40Z")

</div>

The problem with this approach is that it won’t fuse with other dot calls, so you may generate unnecessary loops and temporary arrays.

---

<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: [September 13, 2019, 2:57am UTC](https://discourse.julialang.org/t/neat-way-of-broadcasting-getindex-and-getfield/28706/3 "2019-09-13T02:57:44Z")

</div>

Yep, that is an issue. The cases where I do want to use this syntax tend to be casual scripting where ugly code is worse than the performance hit though. An alternative approach is to find a single-character operator that when prefixed with a `.` lowers to broadcast, such as

```julia
julia> Meta.@lower a.~b
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Base.broadcasted(~, a, b)
│ %2 = Base.materialize(%1)
└── return %2
))))

```

and define this operator to do regular `getindex`. Unfortunately, `...` is already taken so it is not interpreted as broadcasting of the `..` operator

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [September 13, 2019, 3:44am UTC](https://discourse.julialang.org/t/neat-way-of-broadcasting-getindex-and-getfield/28706/4 "2019-09-13T03:44:11Z")

</div>

Wow, this is very creative. One way to avoid generating temporary arrays is to use `broadcasted`:

```julia
using Base.Broadcast: broadcasted
(..)(x, i...) = broadcasted(getindex, x, i...)
(..)(x, i::Union{Symbol, String}) = broadcasted(getfield, x, i)

```

Although you have to write something like `(a..2) .+ 1` and `(b..:re) .+ 1`. You also need to write `identity.(a..2)` or `copy(a..2)` to materialize it…

---

<div class="post-metadata">

### Author: ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)
#### Post date: [October 23, 2020, 6:04pm UTC](https://discourse.julialang.org/t/neat-way-of-broadcasting-getindex-and-getfield/28706/5 "2020-10-23T18:04:43Z")

</div>

What packages resp. community standards/solutions/notations do we currently have for broadcasted getindex on arrays of arrays (to do getindex on the inner arrays)? So, equivalents to `broadcast(getindex, someAoA, Ref(some_idx_or_idx_range))`?

I’m aware that the topic is under discussion as a language feature, but I’m curious which approaches are in use “in the wild” right now.
