# Broadcast Multiplication on Custom Type

**URL:** <https://discourse.julialang.org/t/broadcast-multiplication-on-custom-type/96728>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 28, 2023, 10:04pm UTC](https://discourse.julialang.org/t/broadcast-multiplication-on-custom-type/96728 "2023-03-28T22:04:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgautam03](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tgautam03/32/20545_2.png) [@tgautam03](https://discourse.julialang.org/u/tgautam03)\
**Post date:** [March 28, 2023, 10:04pm UTC](https://discourse.julialang.org/t/broadcast-multiplication-on-custom-type/96728/1 "2023-03-28T22:04:02Z")

</div>

Hi

I’ve created a custom struct named `MyNum` and want to create broadcast operations on this type, like

```julia
a = MyNum(rand(3,2))
b = MyNum(rand(3,2))
c = a .* b

```

I can’t figure out how to do this.

I’ve been able to do this for standard multiplication like

```julia
using Base
function Base.:*(T1::MyNum, T2::MyNum)
    ...
end

```

Hope someone can help me here.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [March 28, 2023, 10:24pm UTC](https://discourse.julialang.org/t/broadcast-multiplication-on-custom-type/96728/2 "2023-03-28T22:24:13Z")

</div>

Broadcasting works on arrays’ elements, so what you have now could work for arrays containing MyNum instances.

If you want to broadcast over MyNum as if it were an `AbstractArray` wrapping the internal array, you need to implement methods in the AbstractArray interface and make a `BroadcastStyle` for your custom type so that the output of broadcasting is still your type instead of `Array`. You don’t need to define new methods for scalar operations like `*`. There is example code for a wrapper array in this section on [broadcasting output type](https://docs.julialang.org/en/v1/manual/interfaces/#Selecting-an-appropriate-output-array), but it may make more sense if you also read the Abstract Arrays and Customizing Broadcasting sections on that page in their entirety.

(Strictly speaking, broadcasting can be done on types other than `AbstractArray`, but if you’re doing work on internal arrays, subtyping `AbstractArray` lets you use existing methods like `iterate` rather than having to re-implement them yourself.)
