Sha256: 0779e547d05005ecfde058b4a918b05a0847253fd6d8e379c2b918c06e4d9800

Contents?: true

Size: 1.32 KB

Versions: 58

Compression:

Stored size: 1.32 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.nil? && b.subtype_of?(a)
        end && returns.subtype_of?(other.returns)
      else
        false
      end
    end
  end
end

Version data entries

58 entries across 58 versions & 1 rubygems

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