# Generalizing Dot Syntax to Broadcast Operators for Arbitrary Types

**URL:** <https://discourse.julialang.org/t/generalizing-dot-syntax-to-broadcast-operators-for-arbitrary-types/25459>\
**Category:** New to Julia\
**Created:** [June 19, 2019, 8:52pm UTC](https://discourse.julialang.org/t/generalizing-dot-syntax-to-broadcast-operators-for-arbitrary-types/25459 "2019-06-19T20:52:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SEA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sea/32/8956_2.png) [@SEA](https://discourse.julialang.org/u/SEA)\
**Post date:** [June 19, 2019, 8:52pm UTC](https://discourse.julialang.org/t/generalizing-dot-syntax-to-broadcast-operators-for-arbitrary-types/25459/1 "2019-06-19T20:52:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 19, 2019, 9:01pm UTC](https://discourse.julialang.org/t/generalizing-dot-syntax-to-broadcast-operators-for-arbitrary-types/25459/2 "2019-06-19T21:01:51Z")

</div>

Check out [Customizing broadcasting](https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting-1).

---

<div class="post-metadata">

**Author:** ![SEA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sea/32/8956_2.png) [@SEA](https://discourse.julialang.org/u/SEA)\
**Post date:** [June 19, 2019, 9:25pm UTC](https://discourse.julialang.org/t/generalizing-dot-syntax-to-broadcast-operators-for-arbitrary-types/25459/3 "2019-06-19T21:25:40Z")

</div>

I hadn’t seen that, thank you for the pointer!

---

<div class="post-metadata">

**Author:** ![SEA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sea/32/8956_2.png) [@SEA](https://discourse.julialang.org/u/SEA)\
**Post date:** [June 20, 2019, 9:07pm UTC](https://discourse.julialang.org/t/generalizing-dot-syntax-to-broadcast-operators-for-arbitrary-types/25459/4 "2019-06-20T21:07:43Z")

</div>

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)
