Module: T::Types::Simple::GenericPatch

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

Instance Method Summary collapse

Instance Method Details

#nameObject

This method intercepts calls to the name method for simple types, so that it can ask the name to the type if the type is generic, since, by this point, we’ve created a clone of that type with the name method returning the appropriate name for that specific concrete type.



76
77
78
79
80
81
82
83
84
85
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 76

def name
  if T::Generic === @raw_type || Tapioca::TypeVariableModule === @raw_type
    # for types that are generic or are type variables, use the name
    # returned by the "name" method of this instance
    @name ||= T.unsafe(@raw_type).name.freeze
  else
    # otherwise, fallback to the normal name lookup
    super
  end
end

#valid?(obj) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tapioca/sorbet_ext/generic_name_patch.rb', line 60

def valid?(obj)
  # Since `Tapioca::TypeVariable` is a `Module`, it will be wrapped by a
  # `Simple` type. We want to always make type variable types valid, so we
  # need to explicitly check that `raw_type` is a `Tapioca::TypeVariable`
  # and return `true`
  if defined?(Tapioca::TypeVariableModule) && Tapioca::TypeVariableModule === @raw_type
    return true
  end

  obj.is_a?(@raw_type)
end