# Sum elements of a struct

**URL:** <https://discourse.julialang.org/t/sum-elements-of-a-struct/80559>\
**Category:** New to Julia\
**Tags:** question, struct, sum\
**Created:** [May 5, 2022, 7:03pm UTC](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559 "2022-05-05T19:03:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vamp](https://avatars.discourse-cdn.com/v4/letter/v/f4b2a3/32.png) [@vamp](https://discourse.julialang.org/u/vamp)\
**Post date:** [May 5, 2022, 7:03pm UTC](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559/1 "2022-05-05T19:03:00Z")

</div>

How can I sum the elements of MyStruct?

Code:

```julia
struct MyStruct
            a::Int 
            b::SVector{N_lds, Int64} 
            c::Int 
        end 

        import Base: -
        -(x::MyStruct, y::MyStruct) = MyStruct(x.a - y.a, x.b .- y.b, x.c - y.c)
        v1 = MyStruct(16, [0, 0, 1], 1)
        v2 = MyStruct(15, [5, 6, 1], 1)
        fg = v1 -v2
        
        sum(fg)

```

ERROR: MethodError: no method matching iterate(::MyStruct)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 5, 2022, 7:09pm UTC](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559/2 "2022-05-05T19:09:38Z")

</div>

What do you want `sum(MyStruct)` to return?

```julia
v2 = MyStruct(15, [5, 6, 1], 1)

```

should it return `28`?

---

<div class="post-metadata">

**Author:** ![vamp](https://avatars.discourse-cdn.com/v4/letter/v/f4b2a3/32.png) [@vamp](https://discourse.julialang.org/u/vamp)\
**Post date:** [May 5, 2022, 7:11pm UTC](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559/3 "2022-05-05T19:11:24Z")

</div>

No, the sum of the elements of fg. Should be -10.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 5, 2022, 7:22pm UTC](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559/4 "2022-05-05T19:22:04Z")

</div>

I mean, should `sum(v2)` return `28`?

---

<div class="post-metadata">

**Author:** ![vamp](https://avatars.discourse-cdn.com/v4/letter/v/f4b2a3/32.png) [@vamp](https://discourse.julialang.org/u/vamp)\
**Post date:** [May 5, 2022, 7:22pm UTC](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559/5 "2022-05-05T19:22:39Z")

</div>

yes

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 5, 2022, 7:25pm UTC](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559/6 "2022-05-05T19:25:37Z")

</div>

Okay, given that you know how to overload `Base.-`, it should be easy to write code to overwrite `Base.sum`.

But if you want `sum` to work automatically, you should write a method for `iterate`. If you were to count through the elements of `MyStruct`, would would that be a meaningful thing to do? In a mathematical sense. This is dependent on what `MyStruct` represents.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 5, 2022, 7:49pm UTC](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559/7 "2022-05-05T19:49:48Z")

</div>

Unless this is a toy example, this is probably not the recommended course of action and will come back to bite you in the future.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 5, 2022, 9:13pm UTC](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559/8 "2022-05-05T21:13:17Z")

</div>

This seems like an [XY problem](https://xyproblem.info/). What are you actually trying to represent with your data structure here?
