Class: TypedRb::Types::Polymorphism::TypeVariable
- Defined in:
- lib/typed/types/polymorphism/type_variable.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bound ⇒ Object
Returns the value of attribute bound.
-
#lower_bound ⇒ Object
Returns the value of attribute lower_bound.
-
#name ⇒ Object
Returns the value of attribute name.
-
#node ⇒ Object
Returns the value of attribute node.
-
#upper_bound ⇒ Object
Returns the value of attribute upper_bound.
-
#variable ⇒ Object
Returns the value of attribute variable.
Instance Method Summary collapse
- #add_constraint(relation, type) ⇒ Object
- #add_message_constraint(message, args) ⇒ Object
- #apply_bindings(bindings_map) ⇒ Object
- #bind(type) ⇒ Object
- #bound_to_generic? ⇒ Boolean
- #check_type(_context) ⇒ Object
- #clean_dynamic_bindings ⇒ Object
- #clone ⇒ Object
- #compatible?(type, relation = :lt) ⇒ Boolean
- #constraints(register = TypingContext) ⇒ Object
- #either? ⇒ Boolean
- #fully_bound? ⇒ Boolean
-
#initialize(var_name, options = {}) ⇒ TypeVariable
constructor
A new instance of TypeVariable.
- #stack_jump? ⇒ Boolean
- #to_s ⇒ Object
- #to_wildcard! ⇒ Object
- #unbind ⇒ Object
- #wildcard? ⇒ Boolean
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, = {}) gen_name = [:gen_name].nil? ? true : [:gen_name] @upper_bound = [:upper_bound] @lower_bound = [:lower_bound] @node = [: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 = [:bound] end |
Instance Attribute Details
#bound ⇒ Object
Returns the value of attribute bound.
5 6 7 |
# File 'lib/typed/types/polymorphism/type_variable.rb', line 5 def bound @bound end |
#lower_bound ⇒ Object
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 |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/typed/types/polymorphism/type_variable.rb', line 5 def name @name end |
#node ⇒ Object
Returns the value of attribute node.
5 6 7 |
# File 'lib/typed/types/polymorphism/type_variable.rb', line 5 def node @node end |
#upper_bound ⇒ Object
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 |
#variable ⇒ Object
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 (, args) return_type = TypingContext.(variable, ) return_type.node = node # add constraint for this add_constraint(:send, args: args, return: return_type, 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
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_bindings ⇒ Object
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 |
#clone ⇒ Object
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
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
23 24 25 |
# File 'lib/typed/types/polymorphism/type_variable.rb', line 23 def either? false end |
#fully_bound? ⇒ 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
19 20 21 |
# File 'lib/typed/types/polymorphism/type_variable.rb', line 19 def stack_jump? false end |
#to_s ⇒ Object
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 |
#unbind ⇒ Object
90 91 92 |
# File 'lib/typed/types/polymorphism/type_variable.rb', line 90 def unbind @bound = nil end |
#wildcard? ⇒ Boolean
69 70 71 |
# File 'lib/typed/types/polymorphism/type_variable.rb', line 69 def wildcard? @wildcard end |