Class: Tapioca::TypeVariableModule

Inherits:
Module
  • Object
show all
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

Constant Summary collapse

DEFAULT_BOUNDS_PROC =

: ^-> Hash[Symbol, untyped]

-> { {} }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#autoload, #autoload_without_tapioca

Constructor Details

#initialize(context, type, variance, bounds_proc) ⇒ TypeVariableModule

: (T::Module context, Type type, Symbol variance, (^-> Hash[Symbol, untyped])? bounds_proc) -> void



137
138
139
140
141
142
143
144
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 137

def initialize(context, type, variance, bounds_proc)
  @context = context
  @type = type
  @variance = variance
  @bounds_proc = bounds_proc || DEFAULT_BOUNDS_PROC

  super()
end

Instance Attribute Details

#typeObject (readonly)

: Type



134
135
136
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 134

def type
  @type
end

Instance Method Details

#coerce_to_type_variableObject

: -> Tapioca::TypeVariable



173
174
175
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 173

def coerce_to_type_variable
  TypeVariable.new(name, @variance)
end

#fixed?Boolean

: -> bool

Returns:

  • (Boolean)


153
154
155
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 153

def fixed?
  bounds.key?(:fixed)
end

#nameObject

: -> String?



147
148
149
150
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 147

def name
  constant_name = super
  constant_name&.split("::")&.last
end

#serializeObject

: -> String



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 158

def serialize
  fixed = bounds[:fixed].to_s if fixed?
  lower = bounds[:lower].to_s if bounds.key?(:lower)
  upper = bounds[:upper].to_s if bounds.key?(:upper)

  RBIHelper.serialize_type_variable(
    @type.serialize,
    @variance,
    fixed,
    upper,
    lower,
  )
end