Skip to content
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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion core/kernel.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,14 @@ 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
Copy link
Contributor

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

Suggested change
| (_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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

Copy link
Contributor

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 boolishes, only accepting true.

Copy link
Contributor

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? 💭

| [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
Comment on lines +619 to +624
Copy link
Contributor

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.


# <!--
# rdoc-file=object.c
Expand Down