-
Notifications
You must be signed in to change notification settings - Fork 221
Open
Description
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
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
soutaro, ybiquitous and KitaitiMakoto
Metadata
Metadata
Assignees
Labels
No labels