Generalizing Dot Syntax to Broadcast Operators for Arbitrary Types

I am posting how to make my example work. Because I am not sub-typing any Array (as in the documentation), I needed to implement “broadcastable” and a binary “BroadcastStyle” constructor to make broadcasting “A” instances with other types (e.g. floats) work. Thanks again for the link!

import Base:BroadcastStyle

function Base.size(x::A{T}) where {T}
return (2,)
end

function Base.getindex(x::A{T}, inds) where {T}
xa = [x.a, x.b]
return xa[inds]
end

function Base.setindex!(x::A{T}, X, inds) where {T}
xa = [x.a, x.b]
xa[inds] = X
x.a = xa[1]
x.b = xa[2]
end

struct MyStyle <: BroadcastStyle end

Base.BroadcastStyle(::Type{<:A{T}}) where {T} = MyStyle()

function Base.similar(bc::Broadcast.Broadcasted{MyStyle}, ::Type{T}) where {T}
return A{T}(T(0), T(0))
end

function Base.broadcastable(x::A{T}) where {T}
return x
end

Base.BroadcastStyle(::MyStyle, ::BroadcastStyle) where {T} = MyStyle()

x = A(1., 2.)
y = A(3., 4.)

println(BroadcastStyle(typeof(x)))
println(Base.broadcastable(x))

println(x .+ y)
println(x .+ .2)
println(.2 .+ x)