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

未关闭
#2,065 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
30/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
ruby
领域
tooling

调研方向

从现有的 _Commentable 接口 workaround 以及本 issue 中提议的 module-self Union 语法开始。审查 RBS 如何表示 module-self 和 Union 类型,然后评估这里提出的类型检查和运行时性能影响。如果提案获批,则完成的标准是拥有一个获接受的设计、实现和测试。

由索引模型根据 Issue 内容生成。

描述

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?

主要语言
Ruby
星标
2.2k
派生
263
平均合并
1 天 20 小时
30 天内合并 PR
64

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ruby/rbs 的其他 Issue

查看 ruby/rbs 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。