Module: Tapioca::GenericTypeRegistry

Extended by:
T::Sig
Defined in:
lib/tapioca/generic_type_registry.rb

Overview

This class is responsible for storing and looking up information related to generic types.

The class stores 2 different kinds of data, in two separate lookup tables:

1. a lookup of generic type instances by name: `@generic_instances`
2. a lookup of type variable serializer by constant and type variable
   instance: `@type_variables`

By storing the above data, we can cheaply query each constant against this registry to see if it declares any generic type variables. This becomes a simple lookup in the ‘@type_variables` hash table with the given constant.

If there is no entry, then we can cheaply know that we can skip generic type information generation for this type.

On the other hand, if we get a result, then the result will be a hash of type variable to type variable serializers. This allows us to associate type variables to the constant names that represent them, easily.

Class Method Summary collapse

Class Method Details

.lookup_type_variables(constant) ⇒ Object



89
90
91
# File 'lib/tapioca/generic_type_registry.rb', line 89

def lookup_type_variables(constant)
  @type_variables[object_id_of(constant)]
end

.register_type(constant, types) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tapioca/generic_type_registry.rb', line 48

def register_type(constant, types)
  # Build the name of the instantiated generic type,
  # something like `"Foo[X, Y, Z]"`
  type_list = types.map { |type| T::Utils.coerce(type).name }.join(", ")
  name = "#{name_of(constant)}[#{type_list}]"

  # Create a generic type with an overridden `name`
  # method that returns the name we constructed above.
  #
  # Also, we try to memoize the generic type based on the name, so that
  # we don't have to keep recreating them all the time.
  @generic_instances[name] ||= create_generic_type(constant, name)
end

.register_type_member(constant, type_member, fixed, lower, upper) ⇒ Object



71
72
73
# File 'lib/tapioca/generic_type_registry.rb', line 71

def register_type_member(constant, type_member, fixed, lower, upper)
  register_type_variable(constant, :type_member, type_member, fixed, lower, upper)
end

.register_type_template(constant, type_template, fixed, lower, upper) ⇒ Object



84
85
86
# File 'lib/tapioca/generic_type_registry.rb', line 84

def register_type_template(constant, type_template, fixed, lower, upper)
  register_type_variable(constant, :type_template, type_template, fixed, lower, upper)
end