Sha256: 5bead55899d6c4a1b53284a4fa22da682254a2b324b11a82bd77d0874f50db0b

Contents?: true

Size: 1.18 KB

Versions: 793

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true
# typed: true

module T::Types
  # Defines the type of a proc (a ruby callable). At runtime, only
  # validates that the value is a `::Proc`.
  #
  # At present, we only support fixed-arity procs with no optional or
  # keyword arguments.
  class Proc < Base
    attr_reader :arg_types
    attr_reader :returns

    def initialize(arg_types, returns)
      @arg_types = {}
      arg_types.each do |key, raw_type|
        @arg_types[key] = T::Utils.coerce(raw_type)
      end
      @returns = T::Utils.coerce(returns)
    end

    # @override Base
    def name
      args = []
      @arg_types.each do |k, v|
        args << "#{k}: #{v.name}"
      end
      "T.proc.params(#{args.join(', ')}).returns(#{returns})"
    end

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

    # @override Base
    private def subtype_of_single?(other)
      case other
      when self.class
        if arg_types.size != other.arg_types.size
          return false
        end
        arg_types.values.zip(other.arg_types.values).all? do |a, b|
          b.subtype_of?(a)
        end && returns.subtype_of?(other.returns)
      else
        false
      end
    end
  end
end

Version data entries

793 entries across 793 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.9172 lib/types/types/proc.rb
sorbet-runtime-0.5.9167 lib/types/types/proc.rb
sorbet-runtime-0.5.9163 lib/types/types/proc.rb
sorbet-runtime-0.5.9161 lib/types/types/proc.rb
sorbet-runtime-0.5.9158 lib/types/types/proc.rb
sorbet-runtime-0.5.9155 lib/types/types/proc.rb
sorbet-runtime-0.5.9154 lib/types/types/proc.rb
sorbet-runtime-0.5.9152 lib/types/types/proc.rb
sorbet-runtime-0.5.9151 lib/types/types/proc.rb
sorbet-runtime-0.5.9147 lib/types/types/proc.rb
sorbet-runtime-0.5.9139 lib/types/types/proc.rb
sorbet-runtime-0.5.9133 lib/types/types/proc.rb
sorbet-runtime-0.5.9130 lib/types/types/proc.rb
sorbet-runtime-0.5.9125 lib/types/types/proc.rb
sorbet-runtime-0.5.9120 lib/types/types/proc.rb
sorbet-runtime-0.5.9115 lib/types/types/proc.rb
sorbet-runtime-0.5.9112 lib/types/types/proc.rb
sorbet-runtime-0.5.9105 lib/types/types/proc.rb
sorbet-runtime-0.5.9104 lib/types/types/proc.rb
sorbet-runtime-0.5.9103 lib/types/types/proc.rb