Bypass inner constructor / call `new` outside inner constructor

Another option is to use reinterpret. I like this method because it emphasizes the danger of bypassing the inner constructor checks:

struct OrderedPair{T <: Real}
    x::T
    y::T
    OrderedPair(x, y) = x > y ? error("out of order") : 
      new{promote_type(typeof(x),typeof(y))}(x,y)
end

with this definition, the normal construction works as expected:

julia> OrderedPair(1,2)
OrderedPair{Int64}(1, 2)

julia> OrderedPair(1,2.2)
OrderedPair{Float64}(1.0, 2.2)

julia> OrderedPair(2,1)
ERROR: out of order

And the unsafe construction can be done as follows:

julia> reinterpret(OrderedPair{Int}, [2,1])[1]
OrderedPair{Int64}(2, 1)
2 Likes