rbs icon indicating copy to clipboard operation
rbs copied to clipboard

How to refine type of generics parameter? And get type of instance of a class generic?

Open TSMMark opened this issue 4 years ago • 0 comments

Consider this contrived example:

class Initializer
  def self.create(klass)
    klass.new
  end
end

How can this class be annotated in rbs such that the return value is known to be an instance of the klass?

Imagining something like the following (which is not valid):

class Initializer[Klass]
  def self.create: (Klass) -> Klass::instance
end

Additionally, how can the Initializer class be annotated such that the Klass generic param must be a subclass of a parent class?

For example, I'm using graphql-ruby batch loaders, whose usage looks like this:

Loaders::Single.for(::Album).load(album_id).then do |album|
                  # ^ ::Album is the class         # ^ album is an instance of Album class

How can Loaders::Single rbs type be defined such that the generic (in the above example ::Album), must be a subclass of Sequel::Model?

TSMMark avatar Mar 17 '21 19:03 TSMMark