Class: TypedRb::Types::Polymorphism::TypeVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/typed/types/polymorphism/type_variable.rb

Direct Known Subclasses

ExistentialTypeVariable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(var_name, options = {}) ⇒ TypeVariable

Returns a new instance of TypeVariable.



7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#boundObject

Returns the value of attribute bound.



5
6
7
# File 'lib/typed/types/polymorphism/type_variable.rb', line 5

def bound
  @bound
end

#lower_boundObject

Returns the value of attribute lower_bound.



5
6
7
# File 'lib/typed/types/polymorphism/type_variable.rb', line 5

def lower_bound
  @lower_bound
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/typed/types/polymorphism/type_variable.rb', line 5

def name
  @name
end

#nodeObject

Returns the value of attribute node.



5
6
7
# File 'lib/typed/types/polymorphism/type_variable.rb', line 5

def node
  @node
end

#upper_boundObject

Returns the value of attribute upper_bound.



5
6
7
# File 'lib/typed/types/polymorphism/type_variable.rb', line 5

def upper_bound
  @upper_bound
end

#variableObject

Returns the value of attribute variable.



5
6
7
# File 'lib/typed/types/polymorphism/type_variable.rb', line 5

def variable
  @variable
end

Instance Method Details

#add_constraint(relation, type) ⇒ Object



27
28
29
30
31
32
33
# 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) ⇒ Object



35
36
37
38
39
40
41
42
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



61
62
63
# File 'lib/typed/types/polymorphism/type_variable.rb', line 61

def bind(type)
  @bound = type
end

#bound_to_generic?Boolean

Returns:

  • (Boolean)


115
116
117
# 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) ⇒ Object



57
58
59
# File 'lib/typed/types/polymorphism/type_variable.rb', line 57

def check_type(_context)
  self
end

#clean_dynamic_bindingsObject



119
120
121
122
123
124
125
126
127
128
129
# 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

#cloneObject



94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# 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) ⇒ Object



53
54
55
# 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?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/typed/types/polymorphism/type_variable.rb', line 23

def either?
  false
end

#fully_bound?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/typed/types/polymorphism/type_variable.rb', line 65

def fully_bound?
  !upper_bound.nil? && !lower_bound.nil?
end

#stack_jump?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/typed/types/polymorphism/type_variable.rb', line 19

def stack_jump?
  false
end

#to_sObject



105
106
107
108
109
110
111
112
113
# 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!Object



73
74
75
# File 'lib/typed/types/polymorphism/type_variable.rb', line 73

def to_wildcard!
  @wildcard = true
end

#unbindObject



90
91
92
# File 'lib/typed/types/polymorphism/type_variable.rb', line 90

def unbind
  @bound = nil
end

#wildcard?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/typed/types/polymorphism/type_variable.rb', line 69

def wildcard?
  @wildcard
end