# Swapping components of structure "nominally"

**URL:** https://discourse.julialang.org/t/swapping-components-of-structure-nominally/78768
**Category:** New to Julia
**Tags:** structarrays
**Created:** [March 30, 2022, 8:56pm UTC](https://discourse.julialang.org/t/swapping-components-of-structure-nominally/78768 "2022-03-30T20:56:26Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)
#### Post date: [March 30, 2022, 8:56pm UTC](https://discourse.julialang.org/t/swapping-components-of-structure-nominally/78768/1 "2022-03-30T20:56:26Z")

</div>

I defined the following struture

```julia
struct AB{T}
A::T
B::T
end

```

with two components of the same numerical type. Sometimes I need to swap the two such that `A=-B` and `B=A`. Is there a way to avoid actual swapping the value and just “mark” the swapping such that subsequent computations will treat `A` as `B` and `-B` as `A`? I’m interested in storing an array of AB type as a `StructArray{AB}`, which makes sense to not actually swap the arrays.

How can this be done? I’m looking for something like the `adjoint(::Matrix...)` markup of numerical arrays. Thank you for any suggestions.

---

<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: [March 30, 2022, 11:58pm UTC](https://discourse.julialang.org/t/swapping-components-of-structure-nominally/78768/2 "2022-03-30T23:58:13Z")

</div>

You can make a specialized wrapper type like:

```julia
julia> struct AB{T}
           A::T
           B::T
       end

julia> struct ABWrapper{T}
           ab::AB{T}
       end

julia> function Base.getproperty(obj :: ABWrapper{T}, field :: Symbol) where {T}
           if field === :A
               obj.ab.B
           elseif field === :B
               -obj.ab.A
           else
               getfield(obj, field)
           end
       end

julia> ab = AB(10, 20)
AB{Int64}(10, 20)

julia> abw = ABWrapper(ab)
ABWrapper{Int64}(AB{Int64}(10, 20))

julia> ab.A
10

julia> abw.A
20

julia> abw.B
-10

```

But I would sincerely just replace the object by one with the fields switched.
