Sha256: a1919c90532cd9e373f457b34c5abfcfe7c9489e57a25b49e1507971b05b4706

Contents?: true

Size: 1.18 KB

Versions: 71

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true
# typed: true

# Specialization of Union for the common case of the union of two simple types.
#
# This covers e.g. T.nilable(SomeModule), T.any(Integer, Float), and T::Boolean.
class T::Private::Types::SimplePairUnion < T::Types::Union
  class DuplicateType < RuntimeError; end

  # @param type_a [T::Types::Simple]
  # @param type_b [T::Types::Simple]
  def initialize(type_a, type_b)
    if type_a == type_b
      raise DuplicateType.new("#{type_a} == #{type_b}")
    end

    @raw_a = type_a.raw_type
    @raw_b = type_b.raw_type
  end

  # @override Union
  def recursively_valid?(obj)
    obj.is_a?(@raw_a) || obj.is_a?(@raw_b)
  end

  # @override Union
  def valid?(obj)
    obj.is_a?(@raw_a) || obj.is_a?(@raw_b)
  end

  # @override Union
  def types
    # We reconstruct the simple types rather than just storing them because
    # (1) this is normally not a hot path and (2) we want to keep the instance
    # variable count <= 3 so that we can fit in a 40 byte heap entry along
    # with object headers.
    @types ||= [
      T::Types::Simple::Private::Pool.type_for_module(@raw_a),
      T::Types::Simple::Private::Pool.type_for_module(@raw_b),
    ]
  end
end

Version data entries

71 entries across 71 versions & 2 rubygems

Version Path
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/sorbet-runtime-0.5.10648/lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10676 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10672 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10667 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10666 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10662 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10658 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10657 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10655 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10649 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10648 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10647 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10644 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10634 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10632 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10628 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10627 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10626 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10625 lib/types/private/types/simple_pair_union.rb
sorbet-runtime-0.5.10624 lib/types/private/types/simple_pair_union.rb