module T::Types::Union::Private::Pool

def self.union_of_types(type_a, type_b, types=EMPTY_ARRAY)

Parameters:
  • types (Array) -- optional array of additional T::Types::Base instances
  • type_b (T::Types::Base) --
  • type_a (T::Types::Base) --
def self.union_of_types(type_a, type_b, types=EMPTY_ARRAY)
  if !types.empty?
    # Slow path
    return Union.new([type_a, type_b] + types)
  elsif !type_a.is_a?(T::Types::Simple) || !type_b.is_a?(T::Types::Simple)
    # Slow path
    return Union.new([type_a, type_b])
  end
  begin
    if type_b == T::Utils::Nilable::NIL_TYPE
      type_a.to_nilable
    elsif type_a == T::Utils::Nilable::NIL_TYPE
      type_b.to_nilable
    else
      T::Private::Types::SimplePairUnion.new(type_a, type_b)
    end
  rescue T::Private::Types::SimplePairUnion::DuplicateType
    # Slow path
    #
    # This shouldn't normally be possible due to static checks,
    # but we can get here if we're constructing a type dynamically.
    #
    # Relying on the duplicate check in the constructor has the
    # advantage that we avoid it when we hit the memoized case
    # of `to_nilable`.
    type_a
  end
end