Structs calling each other

To be clear, if you moved your “converting” constructors outside of the struct block (and replaced new with a call to a constructor) so that they became normal functions, you’d get what you want.

E.g.,

AB(ba::BA) = AB(ba.a, ba.b)
BA(ab::AB) = BA(ab.b, ab.a)

Also, your inner constructor

    function AB(a::Int, b::Int)
        new(a, b)
    end

is basically a duplicate of the default constructor except that it’s less flexible (defining any inner constructor prevents a default constructor from being defined). I would delete it. Inner constructors (functions defined inside a struct block) are only needed in some special cases. For example, you could use an inner constructor to require that any constructed instance satisfies a < b.

3 Likes