Broadcast over . (getfield) in 0.6

question
broadcast

#1

In 0.6 I can do

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:

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

#2

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.


#3

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

getfield.(pairs, :a)

#4

For broadcasting, you need one of two forms, for functions f in general you do f.(args...) or use dot operators, 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.

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

#5

Is there a way to somehow fold the constant?

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> 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 mentioning this, but I did not see any follow-up.