# 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:** 1\
**Showing post:** 4

<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)

---

_[View the full topic](https://discourse.julialang.org/t/generalizing-dot-syntax-to-broadcast-operators-for-arbitrary-types/25459)._
