Sha256: d711f1353adfb75c7a5880bcb199e53339bf199c96a89b9137f3f57635cb808b

Contents?: true

Size: 1.31 KB

Versions: 24

Compression:

Stored size: 1.31 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
    def initialize(arg_types, returns)
      @inner_arg_types = arg_types
      @inner_returns = returns
    end

    def arg_types
      @arg_types ||= @inner_arg_types.map do |key, raw_type|
        [key, T::Utils.coerce(raw_type)]
      end.to_h
    end

    def returns
      @returns ||= T::Utils.coerce(@inner_returns)
    end

    def build_type
      arg_types
      returns
      nil
    end

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

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

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

24 entries across 24 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.11238 lib/types/types/proc.rb
sorbet-runtime-0.5.11237 lib/types/types/proc.rb
sorbet-runtime-0.5.11234 lib/types/types/proc.rb
sorbet-runtime-0.5.11230 lib/types/types/proc.rb
sorbet-runtime-0.5.11226 lib/types/types/proc.rb
sorbet-runtime-0.5.11223 lib/types/types/proc.rb
sorbet-runtime-0.5.11222 lib/types/types/proc.rb
sorbet-runtime-0.5.11221 lib/types/types/proc.rb
sorbet-runtime-0.5.11219 lib/types/types/proc.rb
sorbet-runtime-0.5.11218 lib/types/types/proc.rb
sorbet-runtime-0.5.11216 lib/types/types/proc.rb
sorbet-runtime-0.5.11214 lib/types/types/proc.rb
sorbet-runtime-0.5.11213 lib/types/types/proc.rb
sorbet-runtime-0.5.11212 lib/types/types/proc.rb
sorbet-runtime-0.5.11205 lib/types/types/proc.rb
sorbet-runtime-0.5.11200 lib/types/types/proc.rb
sorbet-runtime-0.5.11193 lib/types/types/proc.rb
sorbet-runtime-0.5.11190 lib/types/types/proc.rb
sorbet-runtime-0.5.11188 lib/types/types/proc.rb
sorbet-runtime-0.5.11181 lib/types/types/proc.rb