Open tk0miya opened 22 hours ago
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 include
ing the module to their own types without tampering your interface.
Your intent is to save the need to create an intermediate module-self
interface.
https://github.com/ruby/rbs/blob/d9000d23c2c1d032a194848562806896c54fd0af/core/comparable.rbs#L55
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. https://github.com/ruby/rbs/blob/d9000d23c2c1d032a194848562806896c54fd0af/core/set.rbs#L200-L202
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:
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.
However, generating an interface dynamically through Union types might negatively impact runtime performance. Do you think this idea is practical?