.. broadcast object property

(post deleted by author)

1 Like

You could also define

(s::Symbol)(data) = getproperty(data, s)

which would let you say

:x.(Array_of_A)

would you mind applying to the above example with X T and T1 ?

Sure:

julia> :T1.(:T.([X...]))  .+ 0
3-element Vector{Int64}:
 1
 2
 2
1 Like

Thanks :slight_smile:

This is type-piracy though, and might have unexpected consequences

Is there a way to improve the definition?

I don’t know of an easy and equally concise way. One way to avoid the piracy is to wrap the Symbol in your own type, eg.

julia> struct MySym
       s :: Symbol
       end

julia> (s::MySym)(data) = getproperty(data, s.s)

julia> MySym(:T1).(MySym(:T).([X...]))  .+ 0
3-element Vector{Int64}:
 1
 2
 2
1 Like

One severe pitfall that hasn’t been mentioned is that .. has a pretty low operator precedence, equal to :. So unfortunately we’re stuck with

julia> :( A..x .^ 2 ) == :( A..(x .^ 2) )
true

Which is probably a good thing, because this confirms that .. was intended by the language designers to be used as a :-like range operator, so it’s unlikely that different packages will use it with wildly different semantics…