# Communication between structures

**URL:** <https://discourse.julialang.org/t/communication-between-structures/110848>\
**Category:** General Usage\
**Created:** [February 27, 2024, 2:33pm UTC](https://discourse.julialang.org/t/communication-between-structures/110848 "2024-02-27T14:33:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 27, 2024, 2:33pm UTC](https://discourse.julialang.org/t/communication-between-structures/110848/1 "2024-02-27T14:33:31Z")

</div>

Suppose I have two structures `A` and `B`. When I instantiate `A` it creates a bunch of arrays say `a`, `b`, and `c` that every instance of `B` needs to performe it’s calculations. Let `x` and `y` be instances of `B`. Then to calculate `x+y` the function `function (+)(x::B,y::B)` needs the values of `a`, `b`, and `c`. What is the most efficient way of getting these value accessed by `function (+)(x::B,y::B)`?

---

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [February 27, 2024, 3:12pm UTC](https://discourse.julialang.org/t/communication-between-structures/110848/2 "2024-02-27T15:12:18Z")

</div>

It seems the best way it to introduce a custom function `plus(x::B, y::B, c::C)` where `C` contains `a, b, c` needed for your calculation. You may also put `C` within every `B` and relly on the fact that it only stores a pointer and thus there are no redundant copies. In some scenarios it is possible to store additional common parameters within a type parameter, but since in your case `a`, `b`, `c` are vectors that does not seem wise as the Julia would need to recompile all methods for different `C`. Also it may be reasonable in your situation to use `C` as a global denoted with `const`.

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 27, 2024, 4:11pm UTC](https://discourse.julialang.org/t/communication-between-structures/110848/3 "2024-02-27T16:11:22Z")

</div>

Thank you. I think I will pass a structure containing all the arrays and other data of `A` every time I instantiate an instance of `B`. One of the arguments of the constructor of `B` will be `A`.
