Class: Tapioca::TypeVariableModule
- Extended by:
- T::Sig
- Defined in:
- lib/tapioca/sorbet_ext/generic_name_patch.rb
Overview
This is subclassing from ‘Module` so that instances of this type will be modules. The reason why we want that is because that means those instances will automatically get bound to the constant names they are assigned to by Ruby. As a result, we don’t need to do any matching of constants to type variables to bind their names, Ruby will do that automatically for us and we get the ‘name` method for free from `Module`.
Defined Under Namespace
Classes: Type
Instance Method Summary collapse
-
#initialize(context, type, variance, fixed, lower, upper) ⇒ TypeVariableModule
constructor
rubocop:disable Metrics/ParameterLists.
- #name ⇒ Object
- #serialize ⇒ Object
Methods inherited from Module
#autoload, #autoload_without_tapioca
Constructor Details
#initialize(context, type, variance, fixed, lower, upper) ⇒ TypeVariableModule
rubocop:disable Metrics/ParameterLists
112 113 114 115 116 117 118 119 120 |
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 112 def initialize(context, type, variance, fixed, lower, upper) # rubocop:disable Metrics/ParameterLists @context = context @type = type @variance = variance @fixed = fixed @lower = lower @upper = upper super() end |
Instance Method Details
#name ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 123 def name constant_name = super # This is a hack to work around modules under anonymous modules not having # names in 2.6 and 2.7: https://bugs.ruby-lang.org/issues/14895 # # This happens when a type variable is declared under `class << self`, for # example. # # The workaround is to give the parent context a name, at which point, our # module gets bound to a name under that name, as well. unless constant_name constant_name = with_bound_name_pre_3_0 { super } end constant_name&.split("::")&.last end |
#serialize ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 142 def serialize parts = [] parts << ":#{@variance}" unless @variance == :invariant parts << "fixed: #{@fixed}" if @fixed parts << "lower: #{@lower}" unless @lower == T.untyped parts << "upper: #{@upper}" unless @upper == BasicObject parameters = parts.join(", ") serialized = @type.serialize.dup serialized << "(#{parameters})" unless parameters.empty? serialized end |