ruby / rbs

Type Signature for Ruby
Other
1.94k stars 211 forks source link

Isolate the case where the argument is Integer in the rand method #1982

Closed rhiroe closed 2 weeks ago

rhiroe commented 1 month ago

Because it always returns Integer for non-zero integer values.

Background

1 + rand(10)

Running steep check, this causes UnresolvedOverloading.

Integer + (Integer | Float)

Writing an expression like this seems to cause UnresolvedOverloading.

Although not a direct solution, this problem can be avoided if the possibility of a Float being returned when the argument is a non-zero integer can be eliminated.

Thank you.

ParadoxV5 commented 1 month ago

0 is still an Integer. To support the concept of non-zero, we’d need #1875.

It’s strange, however, that this situation leads to UnresolvedOverloading. Integer should be able to + either Integer or Float.

Integer + (Integer | Float) -> (Integer | Float)
rhiroe commented 1 month ago

@ParadoxV5

0 is still an Integer

You are right. However, if the argument is 0, Float is returned here. https://github.com/ruby/rbs/blob/9244cd679c2abde430b465a7235f4daa35f94cbc/core/kernel.rbs#L1321 Thus, Integer here should come to a non-zero integer, right? https://github.com/ruby/rbs/blob/9244cd679c2abde430b465a7235f4daa35f94cbc/core/kernel.rbs#L1322

ParadoxV5 commented 1 month ago

It should, but it doesn’t. That’s what #1875 was about.

rhiroe commented 1 month ago

It made sense to me.