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

core: Array#include? and Enumerable#include? should take non-Elem types #2273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tk0miya
Copy link
Contributor

@tk0miya tk0miya commented Feb 11, 2025

For example, Array[Integer] can take other number types validly:

> [1, 2, 3].include? 2.0
=> true
> [1, 2, 3].include? Complex(2)
=> true
> [1, 2, 3].include? Rational(2)
=> true

refs: soutaro/steep#1482

For example, `Array[Integer]` can take other number types validly:

```
> [1, 2, 3].include? 2.0
=> true
> [1, 2, 3].include? Complex(2)
=> true
> [1, 2, 3].include? Rational(2)
=> true
```

refs: soutaro/steep#1482
@ksss
Copy link
Collaborator

ksss commented Feb 12, 2025

In reality, it seems that the == method is being used as an interface.

irb(main):005> TracePoint.new(:a_call) { |tp| p tp }.enable { [1, 2, 3].include? 2.0 }
#<TracePoint:b_call (irb):5>
#<TracePoint:c_call 'include?' (irb):5>
#<TracePoint:c_call '==' (irb):5>
#<TracePoint:c_call '==' (irb):5>
=> true

However, since == is a method of BasicObject, it applies to almost all objects.
Therefore, using untyped is fine.

@@ -2182,7 +2182,7 @@ class Array[unchecked out Elem] < Object
#
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
#
def include?: (Elem object) -> bool
def include?: (untyped object) -> bool
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I thought to allow taking nil value. Therefore I changed this type as follows once:

Suggested change
def include?: (untyped object) -> bool
def include?: (Elem? object) -> bool
| (untyped object) -> false

But I noticed it does not help the numbers' case. Therefore, I used untyped here finally.

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

Successfully merging this pull request may close these issues.

2 participants