Skip to content

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

Open
@tk0miya

Description

@tk0miya

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions