I am trying to make the dot syntax for broadcasting work for a type which I defined.
As an example,
mutable struct A
a
b
end
function broadcast!(f, dest::A, x::A, y::A)
dest.a = f(x.a, y.a)
dest.b = f(x.b, y.b)
end
could make the following expression well defined (x, y of type A)
x .+= y
by calling
broadcast!(+, x, x, y)
But after trying this out, I needed to define “iterate(::A)” to make x .+ y work. I did that by defining:
function iterate(x::A{T}, state::S = 1) where {T, S<:Integer}
if state == 1
return (x.a, 2)
elseif state == 2
return (x.b, 3)
else
return nothing
end
end
But then, the result of (x .+ y) is again an array, not an object of type A. Is there any way I can generalize the “.” behavior for my own types?
I believe this could make dealing with complex types a lot easier.