class T::Types::Proc

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.

Attributes

arg_types[R]
returns[R]

Public Class Methods

new(arg_types, returns) click to toggle source
# File lib/types/types/proc.rb, line 14
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

Public Instance Methods

name() click to toggle source

@override Base

# File lib/types/types/proc.rb, line 23
def name
  args = []
  @arg_types.each do |k, v|
    args << "#{k}: #{v.name}"
  end
  "T.proc.params(#{args.join(', ')}).returns(#{returns})"
end
valid?(obj) click to toggle source

@override Base

# File lib/types/types/proc.rb, line 32
def valid?(obj)
  obj.is_a?(::Proc)
end

Private Instance Methods

subtype_of_single?(other) click to toggle source

@override Base

# File lib/types/types/proc.rb, line 37
        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