ruby / rbs

Type Signature for Ruby
Other
1.96k stars 215 forks source link

Make `rand(Integer)` returns `Integer` #2007

Closed soutaro closed 2 months ago

soutaro commented 2 months ago

Closes https://github.com/ruby/rbs/pull/1982

The rand implementation is a bit annoying for type checkers.

rand(0)    # Returns a Float
rand(1)    # Returns an Integer

The current type definition is sound that rand(1) returns Integer | Float, while it's really annoying because it returns Integer actually.

To make the type checkers more useful and match with our intuition, this PR makes the rand(1) returns Integer.

We assume the following pattern:

The type definition uses literal type 0 to make type of rand(0) call Float. And other Integer cases returns an Integer.

This is unsound, but we believe this is more practical and useful.