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:
When we really want an Float result, we usually pass 0 literal to rand -- rand(0) should return an Float
When we pass some unknown integer value, we usually confirm the value is not 0, because we don't want Float result -- rand(i) can return Integer, no Float
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.
Closes https://github.com/ruby/rbs/pull/1982
The
rand
implementation is a bit annoying for type checkers.The current type definition is sound that
rand(1)
returnsInteger | Float
, while it's really annoying because it returnsInteger
actually.To make the type checkers more useful and match with our intuition, this PR makes the
rand(1)
returnsInteger
.We assume the following pattern:
Float
result, we usually pass0
literal torand
--rand(0)
should return anFloat
0
, because we don't wantFloat
result --rand(i)
can returnInteger
, noFloat
The type definition uses literal type
0
to make type ofrand(0)
callFloat
. And otherInteger
cases returns anInteger
.This is unsound, but we believe this is more practical and useful.