# Broadcast over . (getfield) in 0.6

**URL:** https://discourse.julialang.org/t/broadcast-over-getfield-in-0-6/2335
**Category:** General Usage
**Tags:** question, broadcast
**Created:** [February 27, 2017, 10:17am UTC](https://discourse.julialang.org/t/broadcast-over-getfield-in-0-6/2335 "2017-02-27T10:17:10Z")
**Posts on this page:** 5
**Page:** 1

<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: [February 27, 2017, 10:17am UTC](https://discourse.julialang.org/t/broadcast-over-getfield-in-0-6/2335/1 "2017-02-27T10:17:10Z")

</div>

In `0.6` I can do

```julia
immutable MyType
    a
    b
end
pairs = [MyType(i,2*i) for i in 1:10]
getfield.(pairs, :a)

```

but I can’t figure out the broadcast syntax for `.`, ie how to deal with two dots:

```julia
pairs..a # where to put ()'s? or is this impossible?

```

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [February 27, 2017, 11:30am UTC](https://discourse.julialang.org/t/broadcast-over-getfield-in-0-6/2335/2 "2017-02-27T11:30:45Z")

</div>

what about using comprehension? `[p.a for p in pairs]`. `pairs..a` is a little bit of ambiguous. `pairs` is not a function here, `(x->x.a).(pairs)` is more understandable.

---

<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: [February 27, 2017, 12:24pm UTC](https://discourse.julialang.org/t/broadcast-over-getfield-in-0-6/2335/3 "2017-02-27T12:24:41Z")

</div>

Thanks, but I am interested in broadcasting, ie the syntax that would expand as

```julia
getfield.(pairs, :a)

```

---

<div class="post-metadata">

### Author: ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)
#### Post date: [February 27, 2017, 2:57pm UTC](https://discourse.julialang.org/t/broadcast-over-getfield-in-0-6/2335/4 "2017-02-27T14:57:57Z")

</div>

For broadcasting, you need one of two forms, for functions `f` in general you do `f.(args...)` or use [dot operators](http://docs.julialang.org/en/latest/manual/mathematical-operations.html#man-dot-operators-1), e.g. `a .+ b`. The problem here is that `.` (dot) by itself is not an operator, so there can’t be a dot operator version for it.

If you want a compact form you could still define your own operator, though. E.g.

```julia
↦(s, f) = getfield(s, f)
pairs .↦ :a
pairs .↦ 1

```

---

<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: [May 22, 2017, 2:05pm UTC](https://discourse.julialang.org/t/broadcast-over-getfield-in-0-6/2335/5 "2017-05-22T14:05:50Z")

</div>

Is there a way to somehow fold the constant?

```julia
using BenchmarkTools

↦(s, f) = getfield(s, f)

struct Foo{T}
    a::T
end

two = (Foo(1),Foo(2))

const Both{T} = Tuple{T,T}

function f1(x::Both)
    x.↦:a
end

function f2(x)
    map(x->x.a, x)
end

```

then

```julia
julia> VERSION
v"0.6.0-rc1.0"

julia> @benchmark f1($two)
BenchmarkTools.Trial: 
  memory estimate: 32 bytes
  allocs estimate: 2
  --------------
  minimum time: 35.476 ns (0.00% GC)
  median time: 35.939 ns (0.00% GC)
  mean time: 38.124 ns (1.99% GC)
  maximum time: 754.734 ns (88.53% GC)
  --------------
  samples: 10000
  evals/sample: 990

julia> @benchmark f2($two)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 1.695 ns (0.00% GC)
  median time: 1.971 ns (0.00% GC)
  mean time: 1.969 ns (0.00% GC)
  maximum time: 16.157 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

```

@andyferris commented on [16285](https://github.com/JuliaLang/julia/issues/16285#issuecomment-253656462) mentioning this, but I did not see any follow-up.
