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 Integer #1922

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sampersand
Copy link
Contributor

@sampersand sampersand commented Jul 8, 2024

This PR updates Integer's signature.

@sampersand sampersand force-pushed the swesterman/24-07-04/integer branch from 100d9e3 to 97a7fa1 Compare July 8, 2024 19:50
@sampersand sampersand force-pushed the swesterman/24-07-04/integer branch from 97a7fa1 to 63921c6 Compare July 8, 2024 19:57
@@ -127,7 +127,7 @@ def with_range(start, stop, exclude_end = false)

# `Range` requires `begin <=> end` to return non-nil, but doesn't actually
# end up using the return value of it. This is to add that in when needed.
def lower.<=>(rhs) = :not_nil unless defined? lower.<=>
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 former varient was technically incorrect; the new one now properly defines the singleton funtion only if lower.<=> doesnt exist

@@ -162,6 +162,72 @@
class Numeric
include Comparable

type round_half = string | :up | :down | :even | 'up' | 'down' | 'even'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used in the round functions in Numeric, Integer, and Float. The string at the start allows for _ToStrs

end

# Returns whether `self` is less than `Other`.
interface _OpLt[Other]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, these traits can return anything (like _OpAdd and friends), but the stdlib only returns booleans, so I've made them return bool. We theoretically could do boolish if that's desired, but i'm not a big fan of it

# Performs integer division of `self` by `Other`.
interface _Div[Other]
# Performs the integer division.
def div: (Other other) -> Integer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like the _OpLt and friends, this one and _DivMod also technically return anything, but keeping them inline with ruby standard definitions seems ideal.

@@ -51,6 +51,37 @@ def if_ruby31(&block)
end

module WithStdlibAliases
def with_round_half(&block)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new methods will eventually also be used in Numeric, Float, etc.

@@ -986,15 +967,13 @@ class Integer < Numeric
#
def nobits?: (int mask) -> bool

def nonzero?: () -> self?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oddly enough, nonzero? isn't defined on integers, but zero? is

core/integer.rbs Show resolved Hide resolved
Comment on lines -1267 to -1301
| (2) -> String
| (3) -> String
| (4) -> String
| (5) -> String
| (6) -> String
| (7) -> String
| (8) -> String
| (9) -> String
| (10) -> String
| (11) -> String
| (12) -> String
| (13) -> String
| (14) -> String
| (15) -> String
| (16) -> String
| (17) -> String
| (18) -> String
| (19) -> String
| (20) -> String
| (21) -> String
| (22) -> String
| (23) -> String
| (24) -> String
| (25) -> String
| (26) -> String
| (27) -> String
| (28) -> String
| (29) -> String
| (30) -> String
| (31) -> String
| (32) -> String
| (33) -> String
| (34) -> String
| (35) -> String
| (36) -> String
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like @ParadoxV5 said, presumably these cases were for documentation about which int literals can be given, but they're covered under int, as well arent even TypeErrors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +1297 to +1382
def +@: () -> Integer

%a{steep:deprecated}
def abs2: () -> Integer

%a{steep:deprecated}
def angle: () -> (Integer | Float)

%a{steep:deprecated}
alias arg angle

%a{steep:deprecated}
def conj: () -> Integer

%a{steep:deprecated}
def conjugate: () -> Integer

%a{steep:deprecated}
def dup: () -> self

%a{steep:deprecated}
def eql?: (untyped) -> bool

%a{steep:deprecated}
def finite?: () -> bool

%a{steep:deprecated}
def i: () -> Complex

%a{steep:deprecated}
def imag: () -> Integer

%a{steep:deprecated}
def imaginary: () -> Integer

%a{steep:deprecated}
def infinite?: () -> Integer?

%a{steep:deprecated}
def negative?: () -> bool

%a{steep:deprecated}
def nonzero?: () -> self?

%a{steep:deprecated}
alias phase angle

%a{steep:deprecated}
def polar: () -> [ Integer, Integer | Float ]

%a{steep:deprecated}
def positive?: () -> bool

%a{steep:deprecated}
def quo: (Integer) -> Rational
| (Float) -> Float
| (Rational) -> Rational
| (Complex) -> Complex
| (Numeric) -> Numeric

%a{steep:deprecated}
def real: () -> self

%a{steep:deprecated}
def real?: () -> true

%a{steep:deprecated}
def rect: () -> [ Integer, Numeric ]

%a{steep:deprecated}
alias rectangular rect

%a{steep:deprecated}
def step: () { (Integer) -> void } -> void
| (Numeric limit, ?Integer step) { (Integer) -> void } -> void
| (Numeric limit, ?Numeric step) { (Numeric) -> void } -> void
| (to: Numeric, ?by: Integer) { (Integer) -> void } -> void
| (by: Numeric, ?to: Numeric) { (Numeric) -> void } -> void
| () -> Enumerator[Integer, bot]
| (Numeric limit, ?Integer step) -> Enumerator[Integer, void]
| (Numeric limit, ?Numeric step) -> Enumerator[Numeric, void]
| (to: Numeric, ?by: Integer) -> Enumerator[Integer, void]
| (by: Numeric, ?to: Numeric) -> Enumerator[Numeric, void]

%a{steep:deprecated}
def to_c: () -> Complex
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these are defined on Integer, but instead on Numeric

3.angle()
testing "::Integer"

def with_random_Integers
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using hardcoded integers, I figured generally using a range of integers would cover more cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants