-
Notifications
You must be signed in to change notification settings - Fork 216
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
Updated Kernel#Rational #1438
Updated Kernel#Rational #1438
Conversation
0225acc
to
6b12a17
Compare
Complex, Float and String are already |
6b12a17
to
cc8ebac
Compare
updated to take that into account |
that’s quick … I was looking at the changes and it didn’t quite match your desc 😄 EDIT: Why force-push tho |
So this behavoir is kinda but ambiguously defined. I see you have decided to reflect it in your patch. |
| [T] (Numeric&_RationalDiv[T] numer, Numeric denom, ?exception: bool) -> T | ||
| [T < Numeric] (T value, 1, ?exception: bool) -> T | ||
| (untyped, ?untyped, exception: false) -> nil | ||
interface _RationalDiv[T] | ||
def /: (Numeric) -> T | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested and confirmed, Kernel#Rational
indeed can return non-Rationals, like how the RBS describes. Smells more a Ruby bug than a feature.
@@ -614,7 +614,15 @@ module Kernel : BasicObject | |||
# | |||
# See also String#to_r. | |||
# | |||
def self?.Rational: (Numeric | String | Object x, ?Numeric | String y, ?exception: bool exception) -> Rational | |||
def self?.Rational: (_ToInt | _ToR numer, ?_ToInt | _ToR denom, exception: false) -> Rational? | |||
| (_ToInt | _ToR numer, ?_ToInt | _ToR denom, ?exception: bool) -> Rational |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With exception: false
already covered, can this simply be
| (_ToInt | _ToR numer, ?_ToInt | _ToR denom, ?exception: bool) -> Rational | |
| (_ToInt | _ToR numer, ?_ToInt | _ToR denom, ?exception: true) -> Rational |
?
Also applies to other conversion methods in Kernel.
Does not apply to the [T]
ones if they don’t squelch exceptions as you said.
BTW,
Rational 1, 0, exception: false #=! ZeroDivisionError: divided by 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the : false
case is for when a typechecker can explicitly determine that it's falsey. the : bool
variant is when the typechecker doesn't have enough information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might make sense to "default" to having Rational?
be the default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
: false
case is for when a typechecker can explicitly determine that it's falsey. the: bool
variant is when the typechecker doesn't have enough information.
Not falsey, just false
. Ruby rejects exception: nil
. It also rejects boolish
es, only accepting true
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could there be a world where bool
has three values? 💭
Co-authored-by: Jimmy H <[email protected]>
Consolidated in #1445 |
Updated
Kernel#Rational
to be more accurate.Phew, this one is a mess. In "normal operation," the signature would look like this:
However,
Kernel#Rational
also tries to be a "division operator" for some reason: If the first argument isNumeric
, but defines neitherto_int
norto_i
nor evento_r
,Kernel#Rational
will returnarg1 / arg2
. That is, it'll actually call the/
operator on the first argument, with the second.But it gets worse. If the second argument is exactly
1
, then/
won't even be called.In neither case does
exception: false
actually work—unlike the other conversion methods, exceptions are not caught whenKernel#Rational
is trying to act like a division operator.