Add type constraints to classgenerics in methods

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

未指定实现文件或测试。首先根据 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:

  1. I'm not wed to the syntax—in fact, I'd probably prefer it if it were a part of the [] expression (eg def include?: [T, E < _Equals[T]]). But that might be difficult to implement, and the braces are currently unused before arguments
  2. 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 模板
  • 阅读贡献指南

从这里开始

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

ruby/rbs 的其他 Issue

查看 ruby/rbs 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

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