Open pointlessone opened 3 months ago
If blocks can have overloads – ideally with ruby/rbs#1875, then RBS can guide type checkers on this type narrowing.
Take Enumerable#reject
for a nice example:
def reject: () [F < Elem] { (F) -> false?
| (Elem) -> boolish } -> Array[F]
| () -> ::Enumerator[Elem, ::Array[Elem]]
Relevant methods:
detect
/find
(Enumerable)find_all
(Enumerable)partition
(Enumerable)take_while
/drop_while
(Enumerable, Array)filter
/select
/reject
(Enumerable, Array, Hash, Struct, ENV, DBM)
Suppose we have the following code:
And here's how we might use it:
Steep is having issue with this code:
I guess, this can be justified by the type definition of
Array#select
but this is not exactly useful. Filtering is used all the time to narrow down collection to a subset with certain properties and type is often that property.Is there a way to narrow down the type of the collection? Or do I have to rewrite it with
next unless pet.is_a?(cat) in the
each` block?