Class: Yoda::Typing::Inferencer::TypeBinding

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/typing/inferencer/type_binding.rb

Overview

Bindings of local variables

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil, binds: nil) ⇒ TypeBinding

Returns a new instance of TypeBinding.

Parameters:



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

#bindsHash{ Symbol => Types::Type} (readonly)

Returns:



10
11
12
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 10

def binds
  @binds
end

#parentTypeBinding? (readonly)

Returns:



7
8
9
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 7

def parent
  @parent
end

Instance Method Details

#all_variablesHash{ Symbol => Types::Type }

Returns:



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

Parameters:



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

Parameters:

  • key (String, Symbol)


20
21
22
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 20

def resolve(key)
  all_variables[key.to_sym]
end

#to_hHash{ Symbol => Types::Type }

Returns:



35
36
37
# File 'lib/yoda/typing/inferencer/type_binding.rb', line 35

def to_h
  all_variables
end