Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support type narrowing #1822

Open
mtsmfm opened this issue May 20, 2024 · 2 comments
Open

Support type narrowing #1822

mtsmfm opened this issue May 20, 2024 · 2 comments

Comments

@mtsmfm
Copy link
Contributor

mtsmfm commented May 20, 2024

Ref: soutaro/steep#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
@ParadoxV5
Copy link
Contributor

🆙 for something (not necessarily this)

From soutaro/steep#905 (comment), the current solution is

class Object
  def nil?: () -> bool # cannot be assertively `false` or else `NilClass#nil?` breaks polymorphism of `NilClass < Object`
end
class NilClass
  def nil?: () -> true
end

Or, with #1639 (reply in thread),

class Object
  def nil?: [self < nil] () -> true
          | () -> bool
end

But in either case, the prickly part is that there’s no format to tell that non-nils’ #nil? always return false. In pseudocode, we need:

class Object
  def nil?: [self < nil] () -> true
          | [self !< nil] () -> false # ⬅ a way to declare a negative constraint
end

Alternatively, if the overload semantics changes so that earlier branches subtractively narrow latter branches,

class Object
  def nil?: [self < nil] () -> true
          | () -> false # `else`
end

@ParadoxV5
Copy link
Contributor

https://docs.python.org/3.13/library/typing.html#typing.TypeIs
Y'all were talking about adding pure to RBS for type narrowing predicates before, perhaps this would work instead?
@DanielPechersky, on Ruby Discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants