Sha256: 924757d8296e9fab934e19057f16654095d35968bd8584f6aed396872bfedea1

Contents?: true

Size: 1.23 KB

Versions: 46

Compression:

Stored size: 1.23 KB

Contents

module RBS
  class Substitution
    attr_reader :mapping
    attr_accessor :instance_type

    def empty?
      mapping.empty? && instance_type.nil?
    end

    def initialize()
      @mapping = {}
    end

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

    def self.build(variables, types, instance_type: nil, &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

        subst.instance_type = instance_type
      end
    end

    def apply(ty)
      case ty
      when Types::Variable
        # @type var ty: Types::Variable
        mapping[ty.name] || ty
      when Types::Bases::Instance
        if t = instance_type
          t
        else
          ty
        end
      else
        ty
      end
    end

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

        subst.instance_type = self.instance_type
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
rbs-2.6.0 lib/rbs/substitution.rb
rbs-2.5.1 lib/rbs/substitution.rb
rbs-2.5.0 lib/rbs/substitution.rb
rbs-2.4.0 lib/rbs/substitution.rb
rbs-2.3.2 lib/rbs/substitution.rb
rbs-2.3.1 lib/rbs/substitution.rb
rbs-2.3.0 lib/rbs/substitution.rb
rbs-2.2.2 lib/rbs/substitution.rb
rbs-2.2.1 lib/rbs/substitution.rb
rbs-2.2.0 lib/rbs/substitution.rb
rbs-2.1.0 lib/rbs/substitution.rb
rbs-2.0.0 lib/rbs/substitution.rb
rbs-2.0.0.pre2 lib/rbs/substitution.rb
rbs-2.0.0.pre1 lib/rbs/substitution.rb
rbs-1.8.1 lib/rbs/substitution.rb
rbs-1.8.0 lib/rbs/substitution.rb
rbs-1.7.1 lib/rbs/substitution.rb
rbs-1.7.0 lib/rbs/substitution.rb
rbs-1.7.0.beta.5 lib/rbs/substitution.rb
rbs-1.7.0.beta.4 lib/rbs/substitution.rb