Sha256: 54ea84aafa9fd7c07d9a1585de6ae896635476c016552ea1533fe2a012d7ff74

Contents?: true

Size: 1.41 KB

Versions: 105

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true
# typed: true

module T::Types
  class TypedArray < TypedEnumerable
    # overrides Base
    def name
      "T::Array[#{type.name}]"
    end

    def underlying_class
      Array
    end

    # overrides Base
    def recursively_valid?(obj)
      obj.is_a?(Array) && super
    end

    # overrides Base
    def valid?(obj)
      obj.is_a?(Array)
    end

    def new(*args)
      Array.new(*T.unsafe(args))
    end

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

        @cache = ObjectSpace::WeakMap.new

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

          type = TypedArray.new(mod)

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

    class Untyped < TypedArray
      def initialize
        super(T::Types::Untyped::Private::INSTANCE)
      end

      def valid?(obj)
        obj.is_a?(Array)
      end

      def freeze
        build_type # force lazy initialization before freezing the object
        super
      end

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

Version data entries

105 entries across 105 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.11834 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11829 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11826 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11823 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11820 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11813 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11812 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11810 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11805 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11802 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11801 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11798 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11796 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11789 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11787 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11784 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11781 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11780 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11778 lib/types/types/typed_array.rb
sorbet-runtime-0.5.11770 lib/types/types/typed_array.rb