Skip to content
Closed
Show file tree
Hide file tree
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
322 changes: 166 additions & 156 deletions core/float.rbs

Large diffs are not rendered by default.

127 changes: 70 additions & 57 deletions core/integer.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ class Integer < Numeric
# 10 % 3.0 # => 1.0
# 10 % Rational(3, 1) # => (1/1)
#
def %: (Float) -> Float
| (Rational) -> Rational
| (Integer) -> Integer
| (Numeric) -> Numeric
def %: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Modulo[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand All @@ -192,7 +190,8 @@ class Integer < Numeric
#
# Related: Integer#| (bitwise OR), Integer#^ (bitwise EXCLUSIVE OR).
#
def &: (Integer) -> Integer
def &: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_And[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand All @@ -207,10 +206,8 @@ class Integer < Numeric
# 4 * Rational(1, 3) # => (4/3)
# 4 * Complex(2, 0) # => (8+0i)
#
def *: (Float) -> Float
| (Rational) -> Rational
| (Complex) -> Complex
| (Integer) -> Integer
def *: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Times[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand Down Expand Up @@ -256,10 +253,8 @@ class Integer < Numeric
# 2 ** Rational(-3, 1) # => (1/8)
# -2 ** Rational(-3, 1) # => (-1/8)
#
def **: (Integer) -> Numeric
| (Float) -> Numeric
| (Rational) -> Numeric
| (Complex) -> Complex
def **: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Power[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand All @@ -278,10 +273,8 @@ class Integer < Numeric
#
# 1 + 3.14 # => 4.140000000000001
#
def +: (Integer) -> Integer
| (Float) -> Float
| (Rational) -> Rational
| (Complex) -> Complex
def +: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Add[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand All @@ -296,10 +289,8 @@ class Integer < Numeric
# 4 - Rational(2, 1) # => (2/1)
# 4 - Complex(2, 0) # => (2+0i)
#
def -: (Integer) -> Integer
| (Float) -> Float
| (Rational) -> Rational
| (Complex) -> Complex
def -: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Subtract[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.rb
Expand Down Expand Up @@ -332,10 +323,8 @@ class Integer < Numeric
# 4 / Rational(3, 1) # => (4/3)
# 4 / Complex(3, 0) # => ((4/3)+0i)
#
def /: (Integer) -> Integer
| (Float) -> Float
| (Rational) -> Rational
| (Complex) -> Complex
def /: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Divide[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand All @@ -350,7 +339,8 @@ class Integer < Numeric
# 1 < 0.5 # => false
# 1 < Rational(1, 2) # => false
#
def <: (Numeric) -> bool
def <: (Integer other) -> bool
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_LessThan[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand All @@ -367,7 +357,7 @@ class Integer < Numeric
#
# Related: Integer#>>.
#
def <<: (int) -> Integer
def <<: (int count) -> Integer

# <!--
# rdoc-file=numeric.c
Expand All @@ -384,7 +374,8 @@ class Integer < Numeric
#
# Raises an exception if the comparison cannot be made.
#
def <=: (Numeric) -> bool
def <=: (Integer other) -> bool
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_LessThanOrEqual[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand Down Expand Up @@ -412,8 +403,8 @@ class Integer < Numeric
# Class Integer includes module Comparable, each of whose methods uses
# Integer#<=> for comparison.
#
def <=>: (Integer | Rational) -> Integer
| (untyped) -> Integer?
def <=>: (Integer | Rational other) -> (-1 | 0 | 1)
| (untyped other) -> Integer? # todo: technically uses same coerce mechanism as other methods. That'd get rid of `| Rational`.

# <!-- rdoc-file=numeric.c -->
# Returns `true` if `self` is numerically equal to `other`; `false` otherwise.
Expand Down Expand Up @@ -452,7 +443,8 @@ class Integer < Numeric
#
# Raises an exception if the comparison cannot be made.
#
def >: (Numeric) -> bool
def >: (Integer other) -> bool
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_GreaterThan[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand All @@ -469,7 +461,8 @@ class Integer < Numeric
#
# Raises an exception if the comparison cannot be made.
#
def >=: (Numeric) -> bool
def >=: (Integer other) -> bool
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_GreaterThanOrEqual[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand All @@ -486,7 +479,7 @@ class Integer < Numeric
#
# Related: Integer#<<.
#
def >>: (int) -> Integer
def >>: (int count) -> Integer

# <!--
# rdoc-file=numeric.c
Expand Down Expand Up @@ -527,9 +520,9 @@ class Integer < Numeric
#
# Raises an exception if the slice cannot be constructed.
#
def []: (int) -> Integer
| (int i, int len) -> Integer
| (Range[int]) -> Integer
def []: (int offset) -> (0 | 1)
| (int offset, int size) -> Integer
| (range[int?] range) -> Integer # Technically needs to have things beyond `int` (eg `<=>` and `coerce`), but too cumbersome to annotate

# <!--
# rdoc-file=numeric.c
Expand All @@ -544,7 +537,8 @@ class Integer < Numeric
#
# Related: Integer#& (bitwise AND), Integer#| (bitwise OR).
#
def ^: (Integer) -> Integer
def ^: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Xor[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.rb
Expand Down Expand Up @@ -707,7 +701,8 @@ class Integer < Numeric
#
# 3.ceildiv(1.2) # => 3
#
def ceildiv: (Numeric other) -> Integer
def ceildiv: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[0, _Div[S, RBS::Ops::_UnaryNeg[R]], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand Down Expand Up @@ -742,7 +737,8 @@ class Integer < Numeric
#
# (0x3FFFFFFFFFFFFFFF+1).coerce(42) #=> [42, 4611686018427387904]
#
def coerce: (Numeric) -> [ Numeric, Numeric ]
def coerce: (Integer numeric) -> [Integer, self]
| (_ToF float) -> [Float, Float]

# <!--
# rdoc-file=numeric.rb
Expand Down Expand Up @@ -783,7 +779,13 @@ class Integer < Numeric
#
# Raises an exception if `numeric` does not have method `div`.
#
def div: (Numeric) -> Integer
def div: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[self, _Div[S, R], S] other) -> R

# An interface for defining the `div` method. Used in `Integer#div`
interface _Div[O, R]
def div: (O other) -> R
end

# <!--
# rdoc-file=numeric.c
Expand All @@ -809,10 +811,13 @@ class Integer < Numeric
# 13.divmod(4.0) # => [3, 1.0]
# 13.divmod(Rational(4, 1)) # => [3, (1/1)]
#
def divmod: (Integer) -> [ Integer, Integer ]
| (Float) -> [ Integer, Float ]
| (Rational) -> [ Integer, Rational ]
| (Numeric) -> [ Numeric, Numeric ]
def divmod: (Integer other) -> [ Integer, Integer ]
| [S, R] (Numeric::_Coerce[self, _DivMod[S, R], S] other) -> R

# An interface for defining the `divmod` method. Used in `Integer#divmod`
interface _DivMod[O, R]
def divmod: (O other) -> R
end

# <!--
# rdoc-file=numeric.c
Expand All @@ -832,8 +837,10 @@ class Integer < Numeric
#
# With no block given, returns an Enumerator.
#
def downto: (Numeric limit) { (Integer) -> void } -> Integer
| (Numeric limit) -> ::Enumerator[Integer, self]
def downto: (Integer limit) { (Integer i) -> void } -> self
| (Integer limit) -> Enumerator[Integer, self]
| [S] (Numeric::_Coerce[self, RBS::Ops::_LessThan[S, boolish], S] other) { (Integer i) -> void } -> self
| [S] (Numeric::_Coerce[self, RBS::Ops::_LessThan[S, boolish], S] other) -> Enumerator[Integer, self] # Technically, need more bounds for `size`?

# <!--
# rdoc-file=numeric.rb
Expand All @@ -857,7 +864,13 @@ class Integer < Numeric
#
# Raises an exception if `numeric` cannot be converted to a Float.
#
def fdiv: (Numeric) -> Float
def fdiv: (Integer other) -> Float
| [S] (Numeric::_Coerce[self, _FDiv[S], S] other) -> Float

# An interface for defining the `div` method. Used in `Integer#div`
interface _FDiv[O]
def fdiv: (O other) -> _ToF
end

# <!--
# rdoc-file=numeric.c
Expand Down Expand Up @@ -1073,11 +1086,8 @@ class Integer < Numeric
# a.pow(b) #=> same as a**b
# a.pow(b, m) #=> same as (a**b) % m, but avoids huge temporary values
#
def pow: (Integer other) -> (Integer | Rational)
| (Integer other, Integer modulo) -> Integer
| (Float) -> (Float | Complex)
| (Rational) -> (Float | Rational | Complex)
| (Complex) -> Complex
def pow: (Integer other, ?Integer m) -> Integer
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Power[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.c
Expand Down Expand Up @@ -1130,8 +1140,8 @@ class Integer < Numeric
# 13.remainder(4.0) # => 1.0
# 13.remainder(Rational(4, 1)) # => (1/1)
#
def remainder: (Integer) -> Integer
| (Float) -> Float
def remainder: (Integer other) -> Integer
| (Float) -> Float # TODO: this is technically `| ...`, but these branches need to be here until numeric is fixed.
| (Rational) -> Rational
| (Numeric) -> Numeric

Expand Down Expand Up @@ -1332,8 +1342,10 @@ class Integer < Numeric
#
# With no block given, returns an Enumerator.
#
def upto: (Numeric limit) { (Integer) -> void } -> Integer
| (Numeric limit) -> ::Enumerator[Integer, self]
def upto: (Integer limit) { (Integer i) -> void } -> self
| (Integer limit) -> Enumerator[Integer, self]
| [S] (Numeric::_Coerce[self, RBS::Ops::_GreaterThan[S, boolish], S] other) { (Integer i) -> void } -> self
| [S] (Numeric::_Coerce[self, RBS::Ops::_GreaterThan[S, boolish], S] other) -> Enumerator[Integer, self] # Technically, need more bounds for `size`?

# <!--
# rdoc-file=numeric.rb
Expand All @@ -1356,7 +1368,8 @@ class Integer < Numeric
#
# Related: Integer#& (bitwise AND), Integer#^ (bitwise EXCLUSIVE OR).
#
def |: (Integer) -> Integer
def |: (Integer other) -> Integer
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Or[S, R], S] other) -> R

# <!--
# rdoc-file=numeric.rb
Expand Down
5 changes: 5 additions & 0 deletions core/numeric.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ class Numeric
# The direction you can round
type round_mode = :up | :down | :even | 'up' | 'down' | 'even' | string | nil

# An interface that indicates a type can be coerced.
interface _Coerce[O, Other, Self]
def coerce: (O other) -> [Other, Self]
end

# <!--
# rdoc-file=numeric.c
# - self % other -> real_numeric
Expand Down
Loading
Loading