Sha256: 6dcda7b98897077d335d3373f82b1f841c73617b680fa859a17992012048d71d

Contents?: true

Size: 851 Bytes

Versions: 341

Compression:

Stored size: 851 Bytes

Contents

# frozen_string_literal: true
# typed: true

module T::Types
  # Takes a list of types. Validates that an object matches all of the types.
  class Intersection < Base
    attr_reader :types

    def initialize(types)
      @types = types.flat_map do |type|
        if type.is_a?(Intersection)
          # Simplify nested intersections (mostly so `name` returns a nicer value)
          type.types
        else
          T::Utils.coerce(type)
        end
      end.uniq
    end

    # @override Base
    def name
      "T.all(#{@types.map(&:name).sort.join(', ')})"
    end

    # @override Base
    def valid?(obj)
      @types.all? {|type| type.valid?(obj)}
    end

    # @override Base
    private def subtype_of_single?(other)
      raise "This should never be reached if you're going through `subtype_of?` (and you should be)"
    end

  end
end

Version data entries

341 entries across 341 versions & 1 rubygems

Version Path
sorbet-runtime-0.4.4756 lib/types/types/intersection.rb
sorbet-runtime-0.4.4755 lib/types/types/intersection.rb
sorbet-runtime-0.4.4751 lib/types/types/intersection.rb
sorbet-runtime-0.4.4746 lib/types/types/intersection.rb
sorbet-runtime-0.4.4739 lib/types/types/intersection.rb
sorbet-runtime-0.4.4733 lib/types/types/intersection.rb
sorbet-runtime-0.4.4724 lib/types/types/intersection.rb
sorbet-runtime-0.4.4723 lib/types/types/intersection.rb
sorbet-runtime-0.4.4722 lib/types/types/intersection.rb
sorbet-runtime-0.4.4719 lib/types/types/intersection.rb
sorbet-runtime-0.4.4709 lib/types/types/intersection.rb
sorbet-runtime-0.4.4705 lib/types/types/intersection.rb
sorbet-runtime-0.4.4704 lib/types/types/intersection.rb
sorbet-runtime-0.4.4703 lib/types/types/intersection.rb
sorbet-runtime-0.4.4695 lib/types/types/intersection.rb
sorbet-runtime-0.4.4692 lib/types/types/intersection.rb
sorbet-runtime-0.4.4686 lib/types/types/intersection.rb
sorbet-runtime-0.4.4683 lib/types/types/intersection.rb
sorbet-runtime-0.4.4679 lib/types/types/intersection.rb
sorbet-runtime-0.4.4673 lib/types/types/intersection.rb