Support type narrowing

オープン
#1,822 コメント 2 件 リアクション 6 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
ruby
領域
compilers

調査の方向性

まず、参照されている Sorbet と Steep の実装を読み、次にメソッドシグネチャと制御フローに関連する RBS 構文および型チェックの領域を調査します。nil? や present? などのメソッドを含む、型の絞り込みのためのレビュー済みの構文と動作がサポートされ、テストされれば作業は完了です。

索引モデルが issue の本文から書いたものです。

説明

Ref: https://github.com/soutaro/steep/issues/472

Currently RBS doesn't have any syntax to narrow types in control flow.

So there's no way to achieve the following:

a = [1, nil].sample # a is `Integer | nil`
unless a.nil?
  # a is `Integer` here
  p a + 1
end

But actually, both Sorbet and Steep treat some special methods to narrow types

https://github.com/sorbet/sorbet/blob/718bc64f895abeeff88f56efbc92a3554ffaa4f2/infer/environment.cc#L536-L545

https://github.com/soutaro/steep/blob/a868762c2bd09f0954b05cdd9eab1819e14a51d3/lib/steep/interface/builder.rb#L709-L721

Ideally, I think we need to have a syntax considering Ruby gems which extend Object like ActiveSupport's present?.

I'm not very familiar with type system though, I guess the syntax would be similar with TypeScript's type predicates

https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates

class Object[T?]
  def nil?: () -> self is nil
end
class Object[T?]
  def present?: () -> self is T
end
主要言語
Ruby
スター
2.2k
フォーク
263
平均マージ
1日 20時間
マージ済み PR(30日)
64

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ruby/rbs のほかの issue

ruby/rbs の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。