Module: T::Generic::TypeStoragePatch

Included in:
T::Generic
Defined in:
lib/tapioca/sorbet_ext/generic_name_patch.rb

Overview

This module intercepts calls to generic type instantiations and type variable definitions. Tapioca stores the data from those calls in a ‘GenericTypeRegistry` which can then be used to look up the original call details when we are trying to do code generation.

We are interested in the data of the ‘[]`, `type_member` and `type_template` calls which are all needed to generate good generic information at runtime.

Instance Method Summary collapse

Instance Method Details

#[](*types) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 13

def [](*types)
  # `T::Generic#[]` just returns `self`, so let's call and store it.
  constant = super
  # `register_type` method builds and returns an instantiated clone of the generic type
  # so, we just return that from this method as well.
  Tapioca::Runtime::GenericTypeRegistry.register_type(constant, types)
end

#has_attached_class!(variance = :invariant, &bounds_proc) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 47

def has_attached_class!(variance = :invariant, &bounds_proc)
  Tapioca::Runtime::GenericTypeRegistry.register_type_variable(
    self,
    Tapioca::TypeVariableModule.new(
      T.cast(self, Module),
      Tapioca::TypeVariableModule::Type::HasAttachedClass,
      variance,
      bounds_proc,
    ),
  )
end

#type_member(variance = :invariant, &bounds_proc) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 21

def type_member(variance = :invariant, &bounds_proc)
  # `T::Generic#type_member` just instantiates a `T::Type::TypeMember` instance and returns it.
  # We use that when registering the type member and then later return it from this method.
  Tapioca::TypeVariableModule.new(
    T.cast(self, Module),
    Tapioca::TypeVariableModule::Type::Member,
    variance,
    bounds_proc,
  ).tap do |type_variable|
    Tapioca::Runtime::GenericTypeRegistry.register_type_variable(self, type_variable)
  end
end

#type_template(variance = :invariant, &bounds_proc) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 34

def type_template(variance = :invariant, &bounds_proc)
  # `T::Generic#type_template` just instantiates a `T::Type::TypeTemplate` instance and returns it.
  # We use that when registering the type template and then later return it from this method.
  Tapioca::TypeVariableModule.new(
    T.cast(self, Module),
    Tapioca::TypeVariableModule::Type::Template,
    variance,
    bounds_proc,
  ).tap do |type_variable|
    Tapioca::Runtime::GenericTypeRegistry.register_type_variable(self, type_variable)
  end
end