rbs icon indicating copy to clipboard operation
rbs copied to clipboard

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

Open tk0miya opened this issue 10 months ago • 4 comments

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: https://github.com/soutaro/steep/issues/1482

tk0miya avatar Feb 11 '25 19:02 tk0miya

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.

ksss avatar Feb 12 '25 01:02 ksss

I prefer keeping the strict method type, because finding a code passing an unexpected object to include? actually helps.

Can you share the code where you want this? @tk0miya

soutaro avatar Feb 19 '25 05:02 soutaro

Ah, https://github.com/soutaro/steep/issues/1482...

%w,true,.include?(ENV['foo'])

Hmm...

soutaro avatar Feb 19 '25 05:02 soutaro

No conclusion yet, though I'm not very positive about this change. Skipped in 3.9.

soutaro avatar Feb 27 '25 07:02 soutaro