Sha256: f67703ac243b16445b8d700f75034c944895b188ac563b3cfa898f2b0146e066

Contents?: true

Size: 1.08 KB

Versions: 165

Compression:

Stored size: 1.08 KB

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
    def initialize(types)
      @inner_types = types
    end

    def types
      @types ||= @inner_types.flat_map do |type|
        type = T::Utils.resolve_alias(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

    def build_type
      types
      nil
    end

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

    # overrides Base
    def recursively_valid?(obj)
      types.all? {|type| type.recursively_valid?(obj)}
    end

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

    # overrides 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

165 entries across 165 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.11668 lib/types/types/intersection.rb
sorbet-runtime-0.5.11663 lib/types/types/intersection.rb
sorbet-runtime-0.5.11647 lib/types/types/intersection.rb
sorbet-runtime-0.5.11645 lib/types/types/intersection.rb
sorbet-runtime-0.5.11642 lib/types/types/intersection.rb
sorbet-runtime-0.5.11641 lib/types/types/intersection.rb
sorbet-runtime-0.5.11637 lib/types/types/intersection.rb
sorbet-runtime-0.5.11635 lib/types/types/intersection.rb
sorbet-runtime-0.5.11633 lib/types/types/intersection.rb
sorbet-runtime-0.5.11631 lib/types/types/intersection.rb
sorbet-runtime-0.5.11630 lib/types/types/intersection.rb
sorbet-runtime-0.5.11625 lib/types/types/intersection.rb
sorbet-runtime-0.5.11620 lib/types/types/intersection.rb
sorbet-runtime-0.5.11618 lib/types/types/intersection.rb
sorbet-runtime-0.5.11615 lib/types/types/intersection.rb
sorbet-runtime-0.5.11611 lib/types/types/intersection.rb
sorbet-runtime-0.5.11610 lib/types/types/intersection.rb
sorbet-runtime-0.5.11609 lib/types/types/intersection.rb
sorbet-runtime-0.5.11608 lib/types/types/intersection.rb
sorbet-runtime-0.5.11604 lib/types/types/intersection.rb