class TypedRb::Types::Polymorphism::TypeVariable

Attributes

bound[RW]
lower_bound[RW]
name[RW]
node[RW]
upper_bound[RW]
variable[RW]

Public Class Methods

new(var_name, options = {}) click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 7
def initialize(var_name, options = {})
  gen_name = options[:gen_name].nil? ? true : options[:gen_name]
  @upper_bound = options[:upper_bound]
  @lower_bound = options[:lower_bound]
  @node = options[:node]
  @wildcard = var_name.to_s.end_with?('?')
  var_name.sub!(/:?\?/, '') if @wildcard
  @name = var_name
  @variable = gen_name ? Model::GenSym.next("TV_#{var_name}") : var_name
  @bound = options[:bound]
end

Public Instance Methods

add_constraint(relation, type) click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 27
def add_constraint(relation, type)
  if type.is_a?(TypeVariable) && type.bound
    TypingContext.add_constraint(variable, relation, type.bound)
  else
    TypingContext.add_constraint(variable, relation, type)
  end
end
add_message_constraint(message, args) click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 35
def add_message_constraint(message, args)
  return_type = TypingContext.type_variable_for_message(variable, message)
  return_type.node = node
  # add constraint for this
  add_constraint(:send, args: args, return: return_type, message: message)
  # return return type
  return_type
end
apply_bindings(bindings_map) click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 77
def apply_bindings(bindings_map)
  bound_var = bindings_map[variable]
  if bound_var && bound.nil?
    self.bound = bound_var.bound
    self.upper_bound = bound_var.upper_bound
    self.lower_bound = bound_var.lower_bound
    self.to_wildcard! if bound_var.wildcard?
  elsif bound && (bound.is_a?(TyGenericSingletonObject) || bound.is_a?(TyGenericObject))
     bound.apply_bindings(bindings_map)
  end
  self
end
bind(type) click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 61
def bind(type)
  @bound = type
end
bound_to_generic?() click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 115
def bound_to_generic?
  bound && bound.respond_to?(:generic?) && bound.generic?
end
check_type(_context) click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 57
def check_type(_context)
  self
end
clean_dynamic_bindings() click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 119
def clean_dynamic_bindings
  if @bound.is_a?(TypedRb::Types::TyDynamic)
    @bound = nil
  end
  if @lower_bound.is_a?(TypedRb::Types::TyDynamic)
    @lower_bound = nil
  end
  if @upper_bound.is_a?(TypedRb::Types::TyDynamic)
    @upper_bound = nil
  end
end
clone() click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 94
def clone
  var = TypeVariable.new(variable,
                         node: node,
                         gen_name: false,
                         upper_bound: upper_bound,
                         lower_bound: lower_bound,
                         bound: bound)
  var.to_wildcard! if wildcard?
  var
end
compatible?(type, relation = :lt) click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 44
def compatible?(type, relation = :lt)
  if @bound
    @bound.compatible?(type, relation)
  else
    add_constraint(relation, type)
    true
  end
end
constraints(register = TypingContext) click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 53
def constraints(register = TypingContext)
  register.constraints_for(variable).map { |(t, c)| [self, t, c] }
end
either?() click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 23
def either?
  false
end
fully_bound?() click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 65
def fully_bound?
  !upper_bound.nil? && !lower_bound.nil?
end
stack_jump?() click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 19
def stack_jump?
  false
end
to_s() click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 105
def to_s
  wildcard_part = wildcard? ? '*' : ''
  bound_part = if @bound
                 @bound
               else
                 "[#{lower_bound || '?'},#{upper_bound || '?'}]"
               end
  "#{@variable}#{wildcard_part}::#{bound_part}"
end
to_wildcard!() click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 73
def to_wildcard!
  @wildcard = true
end
unbind() click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 90
def unbind
  @bound = nil
end
wildcard?() click to toggle source
# File lib/typed/types/polymorphism/type_variable.rb, line 69
def wildcard?
  @wildcard
end