# Problem with map() and function.() when using structs

**URL:** https://discourse.julialang.org/t/problem-with-map-and-function-when-using-structs/83697
**Category:** General Usage
**Tags:** question
**Created:** [July 3, 2022, 12:07pm UTC](https://discourse.julialang.org/t/problem-with-map-and-function-when-using-structs/83697 "2022-07-03T12:07:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![shabathq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shabathq/32/37620_2.png) [@shabathq](https://discourse.julialang.org/u/shabathq)
#### Post date: [July 3, 2022, 12:07pm UTC](https://discourse.julialang.org/t/problem-with-map-and-function-when-using-structs/83697/1 "2022-07-03T12:07:27Z")

</div>

Hello!  
I’ve run into this issue on multiple occasions, and I hoped someone here could help me out understand what’s going on.

So after defining instances of a struct and a function that takes input as the struct instance, the function cannot be applied element-wise to a single instance of a struct. The same problem occurs when using map(function, struct\_instance). For vector of struct instances, everything works fine.

If that’s not a bug, how do I avoid having to define two methods of my function, or make an if statement each time I run the function?

```julia
struct test_struct
    a
    b
end

A = test_struct(1, 2)
B = test_struct(1, 2)

function test_func(object::test_struct)
    return object.a + object.b
end 

test_func.(A)
test_func.([A, B])
test_func(A)

map(test_func, A)
map.(test_func, A)
map(test_func, [A, B])
map.(test_func, [A, B])

```

 ![Screenshot 2022-07-03 at 14.11.17](https://global.discourse-cdn.com/julialang/original/3X/d/d/dd4122350f9892878d31f2d19ab8663e52b4e252.png)

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 3, 2022, 12:16pm UTC](https://discourse.julialang.org/t/problem-with-map-and-function-when-using-structs/83697/2 "2022-07-03T12:16:48Z")

</div>

Broadcasting and `map` is for containers - your `test_struct` is not a container. It doesn’t implement the `AbstractArray` interface:

[https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array)

That’s also why the mapping and broadcasting over an array of your struct works - an array has these methods defined.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [July 3, 2022, 1:07pm UTC](https://discourse.julialang.org/t/problem-with-map-and-function-when-using-structs/83697/3 "2022-07-03T13:07:48Z")

</div>

> [@shabathq](#):
>
> ```julia
> struct test_struct
> a
> b
> end
> 
> A = test_struct(1, 2)
> B = test_struct(1, 2)
> 
> function test_func(object::test_struct)
> return object.a + object.b
> end 
> 
> test_func.(A)
> test_func.([A, B])
> test_func(A)
> 
> map(test_func, A)
> map.(test_func, A)
> map(test_func, [A, B])
> map.(test_func, [A, B])
> 
> ```

Adding `Base.broadcastable(x::test_struct) = Ref(x)` will fix your first set of errors. I’m not sure what you are trying to do with the other examples.

---

<div class="post-metadata">

### Author: ![mahmah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mahmah/32/214326_2.png) [@mahmah](https://discourse.julialang.org/u/mahmah)
#### Post date: [March 18, 2026, 5:26am UTC](https://discourse.julialang.org/t/problem-with-map-and-function-when-using-structs/83697/4 "2026-03-18T05:26:18Z")

</div>

This is documented at least here:

> **[Composite Types - Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#Composite-Types)**
>
> Type systems have traditionally fallen into two quite different camps: static type systems, where every program expression must have a type computable before the execution of the program, and dynamic type systems, where nothing is known about types...

I hope someone could add some example to that part of the documentation to make it clear that you cannot broadcast over structs unless you want to do that.
