Class: TypedRb::Types::TyGenericExistentialType

Inherits:
TyExistentialType show all
Includes:
Polymorphism::GenericComparisons, Polymorphism::GenericObject, Polymorphism::GenericVariables, SingletonObject
Defined in:
lib/typed/types/ty_generic_existential_type.rb

Instance Attribute Summary collapse

Attributes inherited from TyObject

#classes, #hierarchy, #modules, #ruby_type, #with_ruby_type

Attributes inherited from Type

#node

Instance Method Summary collapse

Methods included from SingletonObject

#actual_arguments_hash, #apply_type_argument, #apply_type_arguments, #apply_type_arguments_recursively, #clone_with_substitutions

Methods included from Polymorphism::GenericVariables

#type_vars, #unbound_vars

Methods included from Polymorphism::GenericComparisons

#!=, #<, #<=, #==, #>, #>=, #add_type_var_constraint, #check_generic_type_relation, #check_inferior_or_equal_binding, #check_type_var_inclusion, #compatible?, #compatible_free_type_vars?, #incompatible_free_type_vars?, #to_ty_object

Methods included from Polymorphism::GenericObject

#ancestor_of_super_type?, #generic?, #generic_singleton_object, #generic_type_var_to_applied_type_var, #materialize_found_function, #materialize_found_function_arg, #materialize_super_type_found_function, #parse_super_type_materialization_arg, #parse_super_type_materialization_args, #to_s

Methods inherited from TyObject

#<=>, #as_object_type, #check_type, #compatible?, #dynamic?, #either?, #find_function_type, #find_function_type_in_hierarchy, #find_var_type, #generic?, #join, #max, #resolve_ruby_method, #singleton?, #to_s, #union

Methods included from Comparable

#<=>

Methods inherited from Type

#compatible?, #either?, #stack_jump?

Constructor Details

#initialize(ruby_type, type_vars, node = nil) ⇒ TyGenericExistentialType

Returns a new instance of TyGenericExistentialType.



14
15
16
17
# File 'lib/typed/types/ty_generic_existential_type.rb', line 14

def initialize(ruby_type, type_vars, node = nil)
  super(ruby_type, node)
  @type_vars = type_vars
end

Instance Attribute Details

#local_typing_contextObject

Returns the value of attribute local_typing_context.



12
13
14
# File 'lib/typed/types/ty_generic_existential_type.rb', line 12

def local_typing_context
  @local_typing_context
end

#self_variableObject

Returns the value of attribute self_variable.



12
13
14
# File 'lib/typed/types/ty_generic_existential_type.rb', line 12

def self_variable
  @self_variable
end

Instance Method Details

#check_inclusion(self_type) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/typed/types/ty_generic_existential_type.rb', line 19

def check_inclusion(self_type)
  if self_type.generic?
    if ancestor_of_super_type?(self_type.super_type, ruby_type)
      super_type = ancestor_of_super_type?(self_type.super_type, ruby_type)
      materialize(self_type, super_type.type_vars)
    else
      materialize(self_type, self_type.type_vars)
    end
  else
    # not generic extending a generic type
    raise StandardError, "Extending generic module type #{ruby_type} in #{self_type} without matching super annotation"
  end
end

#clean_dynamic_bindingsObject



48
49
50
51
52
53
# File 'lib/typed/types/ty_generic_existential_type.rb', line 48

def clean_dynamic_bindings
  type_vars.each do |type_var|
    type_var.clean_dynamic_bindings
  end
  local_typing_context.clean_dynamic_bindings
end

#materialize(self_type, actual_arguments) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/typed/types/ty_generic_existential_type.rb', line 33

def materialize(self_type, actual_arguments)
  TypedRb.log binding, :debug, "Materialising generic existential type '#{self}' with args [#{actual_arguments.map(&:to_s).join(',')}]"
  compute_minimal_typing_context if @local_typing_context.nil?

  applied_typing_context, substitutions = @local_typing_context.clone(:module_self)
  fresh_vars_generic_type = clone_with_substitutions(substitutions)
  TypingContext.with_context(applied_typing_context) do
    apply_type_arguments(fresh_vars_generic_type, actual_arguments)
    context_self_type = Types::TypingContext.type_variable_for(ruby_type, :module_self, [ruby_type])
    context_self_type.compatible?(self_type, :lt)
  end
  Polymorphism::Unification.new(applied_typing_context.all_constraints).run(false)
  applied_typing_context.unlink # these constraints have already been satisfied
end