Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tk0miya opened this issue Oct 18, 2024 · 1 comment
Open

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

tk0miya opened this issue Oct 18, 2024 · 1 comment

Comments

@tk0miya
Copy link
Contributor

tk0miya commented Oct 18, 2024

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?

@ParadoxV5
Copy link
Contributor

If module-self is a union type, then the types that include this module must be a branch of the union.

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

This is reminiscent of a sealed class in Java, which prevents users from includeing the module to their own types without tampering your interface.


Your intent is to save the need to create an intermediate module-self interface.

module Comparable : _WithSpaceshipOperator

I agree with the cumbersomeness, but the duck-typing nature of Ruby means that these interfaces can also make exact requirements as well as concrete types.

rbs/core/set.rbs

Lines 200 to 202 in d9000d2

def initialize: (_Each[A]) -> untyped
| [X] (_Each[X]) { (X) -> A } -> untyped
| (?nil) -> untyped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants