ruby / ruby/rbs

Proposal: allow to specify Union type for the module-self

Open
#2,065 1 comment 1 reaction 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

It would be very helpful to use Union type for the module-self.

In my project, I have a Commentable concern module used across multiple ActiveRecord models. This Commentable module uses methods commonly defined in the modules where it is included. For example, it references methods like #comments and attributes such as #user_id.

Currently, in RBS, only classes or interfaces can be specified for module-self. As a workaround, I have defined an interface as follows:

interface _Commentable
  def comments: () -> Comment::ActiveRecord_Relation
  def user_id: () -> Integer
  ...
end

module Commentable : _Commentable
end

This interface allows for proper type checking, but as the number of methods grows, the interface definition becomes more cumbersome.

If it were possible to specify a Union type for module-self, it would allow for type definitions of modules without the need to define an interface.

module Commentable : (Article | Comment | ...)
end

However, generating an interface dynamically through Union types might negatively impact runtime performance. Do you think this idea is practical?

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

Start with the existing _Commentable interface workaround and the proposed module-self Union syntax in this issue. Review how RBS represents module-self and Union types, then assess the type-checking and runtime-performance implications raised here. Done means an accepted design with implementation and tests, if the proposal is approved.

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
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.