Sha256: ded6e12c5717f7dbce5cbb6a65c46ea26e85926bdd3255c4d9626ce15b1ae98d

Contents?: true

Size: 1.34 KB

Versions: 169

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true
# typed: true

module T::Types
  # Validates that an object belongs to the specified class.
  class Simple < Base
    attr_reader :raw_type

    def initialize(raw_type)
      @raw_type = raw_type
    end

    # @override Base
    def name
      # Memoize to mitigate pathological performance with anonymous modules (https://bugs.ruby-lang.org/issues/11119)
      #
      # `name` isn't normally a hot path for types, but it is used in initializing a T::Types::Union,
      # and so in `T.nilable`, and so in runtime constructions like `x = T.let(nil, T.nilable(Integer))`.
      @name ||= @raw_type.name.freeze
    end

    # @override Base
    def valid?(obj)
      obj.is_a?(@raw_type)
    end

    # @override Base
    private def subtype_of_single?(other)
      case other
      when Simple
        @raw_type <= other.raw_type
      else
        false
      end
    end

    def to_nilable
      @nilable ||= T::Types::Union.new([self, T::Utils::Nilable::NIL_TYPE])
    end

    module Private
      module Pool
        def self.type_for_module(mod)
          cached = mod.instance_variable_get(:@__as_sorbet_simple_type)
          return cached if cached

          type = Simple.new(mod)
          mod.instance_variable_set(:@__as_sorbet_simple_type, type) unless mod.frozen?
          type
        end
      end
    end
  end
end

Version data entries

169 entries across 169 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.6363 lib/types/types/simple.rb
sorbet-runtime-0.5.6362 lib/types/types/simple.rb
sorbet-runtime-0.5.6361 lib/types/types/simple.rb
sorbet-runtime-0.5.6360 lib/types/types/simple.rb
sorbet-runtime-0.5.6357 lib/types/types/simple.rb
sorbet-runtime-0.5.6353 lib/types/types/simple.rb
sorbet-runtime-0.5.6352 lib/types/types/simple.rb
sorbet-runtime-0.5.6351 lib/types/types/simple.rb
sorbet-runtime-0.5.6349 lib/types/types/simple.rb
sorbet-runtime-0.5.6346 lib/types/types/simple.rb
sorbet-runtime-0.5.6341 lib/types/types/simple.rb
sorbet-runtime-0.5.6338 lib/types/types/simple.rb
sorbet-runtime-0.5.6336 lib/types/types/simple.rb
sorbet-runtime-0.5.6332 lib/types/types/simple.rb
sorbet-runtime-0.5.6330 lib/types/types/simple.rb
sorbet-runtime-0.5.6324 lib/types/types/simple.rb
sorbet-runtime-0.5.6322 lib/types/types/simple.rb
sorbet-runtime-0.5.6318 lib/types/types/simple.rb
sorbet-runtime-0.5.6317 lib/types/types/simple.rb
sorbet-runtime-0.5.6307 lib/types/types/simple.rb