Add type constraints to classgenerics in methods
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
未指定实现文件或测试。首先根据 issue 中的示例,检查 RBS 现有的方法泛型约束处理;完成的标准是确定一种双方认可的语法,并支持在方法签名中对类泛型施加约束,同时覆盖 Array 和 Enumerable 的情况。
由索引模型根据 Issue 内容生成。
描述
RBS currently only lets you add type constraints to generic variables when they're declared. (For class/modules, it looks like class Foo[P < _ToS] and for methods it looks like def print: [T < _ToS] (T) -> T).
I propose expanding the method form of this to support arbitrary constraints of class generics:
class CardboardBox[Contents]
def add: (Contents) -> void
def submerge!: {Contents < _Waterproof} () -> void
end
There's a lot of motivating examples of this in the stdlib. Here's a select few:
class Array[E]
# `Array#join` requires the `E` to respond to `to_s`
def join: {E < _ToS} (?string separator) -> String
# `Array#sum` without a default argument requires `E`
# to coerce with integers
def sum: [R < _Add] {E < _Coerce[Integer, R]} () -> R
| [I < _Add[E, O], O < _Add] (I default) -> O
end
module Enumerable[E] : _Each[E]
# `Enumerable#include?` goes `element == object`,
# which requires that `E` implements `==`, not `T`.
def include?: [T] {E < _Equals[T]} (T object) -> bool
end
Few things to note:
- I'm not wed to the syntax—in fact, I'd probably prefer it if it were a part of the
[]expression (egdef include?: [T, E < _Equals[T]]). But that might be difficult to implement, and the braces are currently unused before arguments - This proposal only talks class generics, as that's a somewhat pressing need. However, it might be nice (and if it's easy to do), extend this syntax to support any form of constraints
- 主要语言
- Ruby
- 星标
- 2.2k
- 派生
- 263
- 平均合并
- 1 天 20 小时
- 30 天内合并 PR
- 64
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ruby/rbs 的其他 Issue
-
InlineParser: `@rbs @ivar` annotation is reported as unused unless it is the first thing in the class body可能已有人在做 @ksss 于 50 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 15/100
维护者通常 1 天内回复
相似的 Issue
-
Pro request form loses all text when the subject is too long可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭bug f:request-creation professional
难度 2/5 1-3 小时 新手友好度 64/100
mysociety/alaveteli#9733 · 1 条评论 ·
维护者通常 1 天内回复
-
needs verification
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 13 天内回复
-
🐛 bug 👩💻 équipe CE 🧑🏭 entreprises
难度 2/5 1-3 小时 新手友好度 76/100
betagouv/conseillers-entreprises#4808 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
-
难度 2/5 1-3 小时 新手友好度 78/100