Class: Yoda::Typing::Inferencer::TypeBinding
- Inherits:
-
Object
- Object
- Yoda::Typing::Inferencer::TypeBinding
- Defined in:
- lib/yoda/typing/inferencer/type_binding.rb
Overview
Bindings of local variables
Instance Attribute Summary collapse
- #binds ⇒ Hash{ Symbol => Types::Type} readonly
- #parent ⇒ TypeBinding? readonly
Instance Method Summary collapse
- #all_variables ⇒ Hash{ Symbol => Types::Type }
- #bind(key, type) ⇒ Object
-
#initialize(parent: nil, binds: nil) ⇒ TypeBinding
constructor
A new instance of TypeBinding.
- #resolve(key) ⇒ Object
- #to_h ⇒ Hash{ Symbol => Types::Type }
Constructor Details
#initialize(parent: nil, binds: nil) ⇒ TypeBinding
Returns a new instance of TypeBinding.
14 15 16 17 |
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 14 def initialize(parent: nil, binds: nil) @parent = parent @binds = (binds || {}).to_h end |
Instance Attribute Details
#binds ⇒ Hash{ Symbol => Types::Type} (readonly)
10 11 12 |
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 10 def binds @binds end |
#parent ⇒ TypeBinding? (readonly)
7 8 9 |
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 7 def parent @parent end |
Instance Method Details
#all_variables ⇒ Hash{ Symbol => Types::Type }
40 41 42 |
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 40 def all_variables (parent&.all_variables || {}).merge(binds) end |
#bind(key, type) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 26 def bind(key, type) key = key.to_sym type = (type.is_a?(Symbol) && resolve(type)) || type @binds.transform_values! { |value| value == key ? type : value } @binds[key] = type self end |
#resolve(key) ⇒ Object
20 21 22 |
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 20 def resolve(key) all_variables[key.to_sym] end |
#to_h ⇒ Hash{ Symbol => Types::Type }
35 36 37 |
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 35 def to_h all_variables end |