# Making broadcast work for own type

**URL:** <https://discourse.julialang.org/t/making-broadcast-work-for-own-type/7195>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [November 20, 2017, 5:25pm UTC](https://discourse.julialang.org/t/making-broadcast-work-for-own-type/7195 "2017-11-20T17:25:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Post date:** [November 20, 2017, 5:25pm UTC](https://discourse.julialang.org/t/making-broadcast-work-for-own-type/7195/1 "2017-11-20T17:25:56Z")

</div>

If I have my own type, which can be indexed like an array, and has a length. How can I get broadcast to work with it as an array-like type instead of a scalar? I can only find useage guides in the language manual for vectorised ops and broadcasting, nothing about extending the broadcast method to new types.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [November 20, 2017, 5:40pm UTC](https://discourse.julialang.org/t/making-broadcast-work-for-own-type/7195/2 "2017-11-20T17:40:23Z")

</div>

I’m a little fuzzy on exactly what you are trying to do, but you have several options.

First of all, as I understand it, `broadcast` methods are intended for cases where there is an actual underlying loop. If your method is abstract, and does not involve a loop, it is better to write dedicated, undotted, methods. Most of the time the right thing to do is only to overload undotted methods for individual objects, and use the automatic promotion provided by `broadcast` rather than overloading it.

That said, if you want to actually overload a dotted operation, you should do

```julia
broadcast(::typeof(op), arg) = # here is your loop over elements of arg

```

With this method defined, you can then do `op.(arg)` and it will execute your method.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 20, 2017, 5:43pm UTC](https://discourse.julialang.org/t/making-broadcast-work-for-own-type/7195/3 "2017-11-20T17:43:40Z")

</div>

The easiest way would be to make your type `<: AbstractArray` and define size and getindex. That should enable broadcasting to just work. Or is your type not sufficiently array-like for it to be an AbstractArray?

---

<div class="post-metadata">

**Author:** ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Post date:** [November 20, 2017, 6:14pm UTC](https://discourse.julialang.org/t/making-broadcast-work-for-own-type/7195/4 "2017-11-20T18:14:55Z")

</div>

My type does many things an array (actually more like a vector) does, insofar as indexing and having length and eltype and so on. So it has those behaviours, but also a lot more that means it would not be neatly bike-shedded under AbstractArray. I’d like to know what one would do to allow broadcasting (vectorised dot operations) in that situation. Kind-of how say a string has many array (of char) like properties, but crucially it is not an array in the type hierarchy (and also does not support vectorised dot operations AFAIK).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 20, 2017, 6:31pm UTC](https://discourse.julialang.org/t/making-broadcast-work-for-own-type/7195/5 "2017-11-20T18:31:37Z")

</div>

Even though the code changed a bit, most of this is still relevant:

> [@Broadcast for custom type](https://discourse.julialang.org/t/broadcast-for-custom-type/3497/):
>
> I am using v0.6, this is an MWE of a question I want to solve. I would like to make broadcast work for a custom type struct CongruentVector{Td,Tv} descriptor::Td vector::Tv end the following way: given a method ≅ with fallback ≅(a, b) = a == b I would test that all CongruentVector arguments to broadcast have descriptors that are ≅, then broadcast on their vectors and nothing else, ie every other argument would be treated as a “scalar”. The idea behind this interface is that it woul…
