Skip to content

Commit

Permalink
strip tapioca support for runtime generic
Browse files Browse the repository at this point in the history
  • Loading branch information
gtrias committed Sep 10, 2024
1 parent e2852a7 commit f53cdfc
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions lib/runtime_generic.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
# typed: true

require 'tapioca'
require 'tapioca/gem/listeners/base'
require 'tapioca/sorbet_ext/generic_name_patch'
require 'tapioca/runtime/reflection'
require 'tapioca/runtime/generic_type_registry'
require 'tapioca/gem/listeners/sorbet_type_variables'

# This module allows using type introspection to serialize/deserialize custom generics in
# T::Structs.
#
Expand All @@ -20,23 +13,37 @@ module RuntimeGeneric
extend T::Sig
include T::Generic

class TypedGeneric < T::Types::Simple
extend T::Sig

def name
"#{raw_type.name}[#{inner_type.name}]"
end

def initialize(raw_type, inner_type)
super(raw_type)
@inner_type = inner_type
end

attr_reader :inner_type
end

class MyTypeMember < T::Types::TypeMember
def initialize(variance, &type_proc)
super(variance)
@type_proc = type_proc
end

def inner_type
@inner_type ||= @type_proc.call
end
end

def [](inner_type)
T::Types::Simple.new(inner_type)
RuntimeGeneric::TypedGeneric.new(self, inner_type)
end

def type_member(variance = :invariant, &blk)
# `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.
#
# Dear developer, This part was adapted from tapioca so it can keep
# generating proper RBIs for this ad-hoc generics, Genar
Tapioca::TypeVariableModule.new(
T.cast(self, Module),
Tapioca::TypeVariableModule::Type::Member,
variance,
blk
).tap do |type_variable|
Tapioca::Runtime::GenericTypeRegistry.register_type_variable(self, type_variable)
end
MyTypeMember.new(variance, &blk)
end
end

0 comments on commit f53cdfc

Please sign in to comment.