Sha256: b4dae410971657c93896ff6326f2086b1bdd9218a1eddaf72fbb4758c7822460

Contents?: true

Size: 1.53 KB

Versions: 147

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
# typed: true

module T::Types
  class TypedClass < T::Types::Base
    attr_reader :type

    def initialize(type)
      @type = T::Utils.coerce(type)
    end

    # overrides Base
    def name
      "T::Class[#{@type.name}]"
    end

    def underlying_class
      Class
    end

    # overrides Base
    def valid?(obj)
      Class.===(obj)
    end

    # overrides Base
    private def subtype_of_single?(type)
      case type
      when TypedClass
        # treat like generics are erased
        true
      when Simple
        Class <= type.raw_type
      else
        false
      end
    end

    module Private
      module Pool
        CACHE_FROZEN_OBJECTS =
          begin
            ObjectSpace::WeakMap.new[1] = 1
            true # Ruby 2.7 and newer
          rescue ArgumentError
            false # Ruby 2.6 and older
          end

        @cache = ObjectSpace::WeakMap.new

        def self.type_for_module(mod)
          cached = @cache[mod]
          return cached if cached

          type = TypedClass.new(mod)

          if CACHE_FROZEN_OBJECTS || (!mod.frozen? && !type.frozen?)
            @cache[mod] = type
          end
          type
        end
      end
    end

    class Untyped < TypedClass
      def initialize
        super(T.untyped)
      end

      module Private
        INSTANCE = Untyped.new.freeze
      end
    end

    class Anything < TypedClass
      def initialize
        super(T.anything)
      end

      module Private
        INSTANCE = Anything.new.freeze
      end
    end
  end
end

Version data entries

147 entries across 141 versions & 2 rubygems

Version Path
sorbet-runtime-0.5.11170 lib/types/types/typed_class.rb
sorbet-runtime-0.5.11164 lib/types/types/typed_class.rb
sorbet-runtime-0.5.11163 lib/types/types/typed_class.rb
sorbet-runtime-0.5.11162 lib/types/types/typed_class.rb
sorbet-runtime-0.5.11158 lib/types/types/typed_class.rb
sorbet-runtime-0.5.11156 lib/types/types/typed_class.rb
sorbet-runtime-0.5.11155 lib/types/types/typed_class.rb
sorbet-runtime-0.5.11152 lib/types/types/typed_class.rb
sorbet-runtime-0.5.11151 lib/types/types/typed_class.rb
sorbet-runtime-0.5.11150 lib/types/types/typed_class.rb
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/sorbet-runtime-0.5.11108/lib/types/types/typed_class.rb
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/sorbet-runtime-0.5.11104/lib/types/types/typed_class.rb
sorbet-runtime-0.5.11148 lib/types/types/typed_class.rb
sorbet-runtime-0.5.11147 lib/types/types/typed_class.rb
study_line-0.1.5 vendor/bundle/ruby/3.2.0/gems/sorbet-runtime-0.5.11108/lib/types/types/typed_class.rb
study_line-0.1.5 vendor/bundle/ruby/3.2.0/gems/sorbet-runtime-0.5.11104/lib/types/types/typed_class.rb
study_line-0.1.4 vendor/bundle/ruby/3.2.0/gems/sorbet-runtime-0.5.11108/lib/types/types/typed_class.rb
study_line-0.1.4 vendor/bundle/ruby/3.2.0/gems/sorbet-runtime-0.5.11104/lib/types/types/typed_class.rb
sorbet-runtime-0.5.11146 lib/types/types/typed_class.rb
study_line-0.1.3 vendor/bundle/ruby/3.2.0/gems/sorbet-runtime-0.5.11104/lib/types/types/typed_class.rb