Sha256: 62df874c19ed41f312acc821cbdacef8ef1a3c487c95d4c5c1050b4d6465529f

Contents?: true

Size: 990 Bytes

Versions: 3

Compression:

Stored size: 990 Bytes

Contents

module Ruby
  module Signature
    class Substitution
      attr_reader :mapping

      def initialize()
        @mapping = {}
      end

      def add(from:, to:)
        mapping[from] = to
      end

      def self.build(variables, types, &block)
        unless variables.size == types.size
          raise "Broken substitution: variables=#{variables}, types=#{types}"
        end

        mapping = variables.zip(types).to_h

        self.new.tap do |subst|
          mapping.each do |v, t|
            type = block_given? ? yield(t) : t
            subst.add(from: v, to: type)
          end
        end
      end

      def apply(ty)
        case ty
        when Types::Variable
          mapping[ty.name] || ty
        else
          ty
        end
      end

      def without(*vars)
        self.class.new.tap do |subst|
          subst.mapping.merge!(mapping)
          vars.each do |var|
            subst.mapping.delete(var)
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
steep-0.14.0 vendor/ruby-signature/lib/ruby/signature/substitution.rb
steep-0.13.0 vendor/ruby-signature/lib/ruby/signature/substitution.rb
steep-0.12.0 vendor/ruby-signature/lib/ruby/signature/substitution.rb