class TypedRb::Types::TypingContext
Attributes
function_context[R]
Public Class Methods
add_constraint(variable, relation, type)
click to toggle source
# File lib/typed/typing_context.rb, line 97 def add_constraint(variable, relation, type) type_variables_register.add_constraint(variable, relation, type) end
all_constraints()
click to toggle source
# File lib/typed/typing_context.rb, line 85 def all_constraints type_variables_register.all_constraints end
all_variables()
click to toggle source
# File lib/typed/typing_context.rb, line 89 def all_variables type_variables_register.all_variables end
bound_generic_type_var?(type_variable)
click to toggle source
# File lib/typed/typing_context.rb, line 113 def bound_generic_type_var?(type_variable) type_variables_register.bound_generic_type_var?(type_variable) end
clear(type)
click to toggle source
# File lib/typed/typing_context.rb, line 140 def clear(type) @type_variables_register = Polymorphism::TypeVariableRegister.new(type) end
constraints_for(variable)
click to toggle source
# File lib/typed/typing_context.rb, line 101 def constraints_for(variable) type_variables_register.constraints[variable] || [] end
duplicate(within_context)
click to toggle source
# File lib/typed/typing_context.rb, line 105 def duplicate(within_context) current_parent = type_variables_register.parent type_variables_register.parent = nil duplicated = Marshal.load(Marshal.dump(within_context)) type_variables_register.parent = current_parent duplicated end
empty_typing_context()
click to toggle source
# File lib/typed/typing_context.rb, line 39 def empty_typing_context Polymorphism::TypeVariableRegister.new(nil, :local) end
find_namespace(constant, namespace = self.namespace)
click to toggle source
# File lib/typed/typing_context.rb, line 18 def find_namespace(constant, namespace = self.namespace) return Class.for_name(constant) if constant.start_with?('::') Class.for_name(namespace.join('::') + '::' + constant) rescue NameError => e if namespace.empty? raise e else find_namespace(constant, namespace.take(namespace.size - 1)) end end
function_context_pop()
click to toggle source
# File lib/typed/typing_context.rb, line 35 def function_context_pop @function_context = nil end
function_context_push(type, message, args)
click to toggle source
# File lib/typed/typing_context.rb, line 29 def function_context_push(type, message, args) @function_context = [type, message, args] end
include?(variable)
click to toggle source
# File lib/typed/typing_context.rb, line 93 def include?(variable) type_variables_register.include?(variable) end
local_type_variable()
click to toggle source
# File lib/typed/typing_context.rb, line 81 def local_type_variable type_variables_register.local_type_variable end
namespace()
click to toggle source
# File lib/typed/typing_context.rb, line 5 def namespace @namespace ||= [] end
namespace_pop()
click to toggle source
# File lib/typed/typing_context.rb, line 14 def namespace_pop @namespace.pop end
namespace_push(constant)
click to toggle source
# File lib/typed/typing_context.rb, line 9 def namespace_push(constant) parts = constant.split('::') @namespace += parts.reject { |part| namespace.include?(part) } end
new(parent = nil)
click to toggle source
# File lib/typed/typing_context.rb, line 168 def initialize(parent = nil) @parent = parent @bindings = {} end
pop_context()
click to toggle source
# File lib/typed/typing_context.rb, line 124 def pop_context fail StandardError, 'Empty typing context stack, impossible to pop' if @type_variables_register.nil? last_register = type_variables_register @type_variables_register = @type_variables_register.parent @type_variables_register.children.reject! { |child| child == last_register } last_register end
push_context(type)
click to toggle source
# File lib/typed/typing_context.rb, line 117 def push_context(type) new_register = Polymorphism::TypeVariableRegister.new(type_variables_register, type) @type_variables_register.children << new_register @type_variables_register = new_register new_register end
top_level()
click to toggle source
work with types
# File lib/typed/typing_context.rb, line 164 def self.top_level TypingContext.new.add_binding!(:self, TyTopLevelObject.new) end
type_variable_for(type, variable, hierarchy)
click to toggle source
# File lib/typed/typing_context.rb, line 47 def type_variable_for(type, variable, hierarchy) type_variables_register.type_variable_for(type, variable, hierarchy) end
type_variable_for_abstraction(abs_kind, variable, context)
click to toggle source
# File lib/typed/typing_context.rb, line 59 def type_variable_for_abstraction(abs_kind, variable, context) type_variables_register.type_variable_for_abstraction(abs_kind, variable, context) end
type_variable_for_function_type(type_var)
click to toggle source
# File lib/typed/typing_context.rb, line 63 def type_variable_for_function_type(type_var) type_variables_register.type_variable_for_generic_type(type_var, true) { :lt => :upper_bound, :gt => :lower_bound }.each do |relation, bound| if type_var.send(bound) value = if type_var.send(bound).is_a?(TyGenericSingletonObject) type_var.send(bound).clone else type_var.send(bound) end type_var.compatible?(value, relation) end end end
type_variable_for_generic_type(type_var)
click to toggle source
# File lib/typed/typing_context.rb, line 77 def type_variable_for_generic_type(type_var) type_variables_register.type_variable_for_generic_type(type_var) end
type_variable_for_global(variable)
click to toggle source
# File lib/typed/typing_context.rb, line 51 def type_variable_for_global(variable) type_variables_register.type_variable_for_global(variable) end
type_variable_for_message(variable, message)
click to toggle source
# File lib/typed/typing_context.rb, line 55 def type_variable_for_message(variable, message) type_variables_register.type_variable_for_message(variable, message) end
type_variables_register()
click to toggle source
# File lib/typed/typing_context.rb, line 43 def type_variables_register @type_variables_register ||= Polymorphism::TypeVariableRegister.new(nil, :top_level) end
vars_info(level)
click to toggle source
# File lib/typed/typing_context.rb, line 144 def vars_info(level) method_registry = type_variables_register while !method_registry.nil? && method_registry.kind != level method_registry = method_registry.parent end if method_registry method_registry.type_variables_register.map do |(key, type_var)| type_var if key.first == :generic end.compact.each_with_object({}) do |type_var, acc| var_name = type_var.variable.split(':').last acc["[#{var_name}]"] = type_var end else {} end end
with_context(context) { || ... }
click to toggle source
# File lib/typed/typing_context.rb, line 132 def with_context(context) old_context = @type_variables_register @type_variables_register = context result = yield @type_variables_register = old_context result end
Public Instance Methods
add_binding(val, type)
click to toggle source
# File lib/typed/typing_context.rb, line 173 def add_binding(val, type) TypingContext.new(self).push_binding(val, type) end
add_binding!(val, type)
click to toggle source
# File lib/typed/typing_context.rb, line 177 def add_binding!(val, type) push_binding(val, type) end
context_name()
click to toggle source
# File lib/typed/typing_context.rb, line 194 def context_name get_self.to_s end
get_self()
click to toggle source
# File lib/typed/typing_context.rb, line 190 def get_self get_type_for('self') end
get_type_for(val)
click to toggle source
# File lib/typed/typing_context.rb, line 181 def get_type_for(val) type = @bindings[val.to_s] if type.nil? @parent.get_type_for(val) if @parent else type end end
Protected Instance Methods
push_binding(val, type)
click to toggle source
# File lib/typed/typing_context.rb, line 200 def push_binding(val, type) @bindings[val.to_s] = type self end