Sha256: 0c1a146856e46f189e2471a32aa68157fcfae4cc6000f9d7b2ee7c1c57879fb6

Contents?: true

Size: 1.31 KB

Versions: 84

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.transform_values do |raw_type|
        T::Utils.coerce(raw_type)
      end
    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

84 entries across 84 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.11495 lib/types/types/proc.rb
sorbet-runtime-0.5.11493 lib/types/types/proc.rb
sorbet-runtime-0.5.11492 lib/types/types/proc.rb
sorbet-runtime-0.5.11491 lib/types/types/proc.rb
sorbet-runtime-0.5.11487 lib/types/types/proc.rb
sorbet-runtime-0.5.11485 lib/types/types/proc.rb
sorbet-runtime-0.5.11481 lib/types/types/proc.rb
sorbet-runtime-0.5.11480 lib/types/types/proc.rb
sorbet-runtime-0.5.11478 lib/types/types/proc.rb
sorbet-runtime-0.5.11475 lib/types/types/proc.rb
sorbet-runtime-0.5.11473 lib/types/types/proc.rb
sorbet-runtime-0.5.11471 lib/types/types/proc.rb
sorbet-runtime-0.5.11466 lib/types/types/proc.rb
sorbet-runtime-0.5.11465 lib/types/types/proc.rb
sorbet-runtime-0.5.11463 lib/types/types/proc.rb
sorbet-runtime-0.5.11460 lib/types/types/proc.rb
sorbet-runtime-0.5.11450 lib/types/types/proc.rb
sorbet-runtime-0.5.11447 lib/types/types/proc.rb
sorbet-runtime-0.5.11444 lib/types/types/proc.rb
sorbet-runtime-0.5.11439 lib/types/types/proc.rb