123456789_123456789_123456789_123456789_123456789_

Class: RBS::Substitution

Relationships & Source Files
Inherits: Object
Defined in: lib/rbs/substitution.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newSubstitution

[ GitHub ]

  
# File 'lib/rbs/substitution.rb', line 10

def initialize()
  @mapping = {}
end

Class Method Details

.build(variables, types, instance_type: nil, &block)

[ GitHub ]

  
# File 'lib/rbs/substitution.rb', line 18

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

Instance Attribute Details

#empty?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/substitution.rb', line 6

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

#instance_type (rw)

[ GitHub ]

  
# File 'lib/rbs/substitution.rb', line 4

attr_accessor :instance_type

#mapping (readonly)

[ GitHub ]

  
# File 'lib/rbs/substitution.rb', line 3

attr_reader :mapping

Instance Method Details

#add(from:, to:)

[ GitHub ]

  
# File 'lib/rbs/substitution.rb', line 14

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

#apply(ty)

[ GitHub ]

  
# File 'lib/rbs/substitution.rb', line 35

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

#without(*vars)

[ GitHub ]

  
# File 'lib/rbs/substitution.rb', line 51

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