Sha256: 566cda528b0c8568cf62770f8021fecf227fba5af27c2c57b3aaf175d1daaed6

Contents?: true

Size: 890 Bytes

Versions: 6

Compression:

Stored size: 890 Bytes

Contents

module RBS
  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

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
rbs-0.4.0 lib/rbs/substitution.rb
rbs-0.3.1 lib/rbs/substitution.rb
rbs-0.3.0 lib/rbs/substitution.rb
rbs-0.2.0 lib/rbs/substitution.rb
steep-0.16.0 vendor/ruby-signature/lib/rbs/substitution.rb
steep-0.15.0 vendor/ruby-signature/lib/rbs/substitution.rb