ruby / ruby/rbs

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

Open
#655 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.2k
Forks
256
Avg merge
6d 17h
Merged PRs (30d)
37

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or entry points are named. Start by tracing RBS generic signature syntax for class instances and subtype constraints; done would be a documented, validated way to express both requested relationships.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.