|
162 | 162 | class Numeric
|
163 | 163 | include Comparable
|
164 | 164 |
|
| 165 | + # Type alias for the `half` parameter in `round` functions. |
| 166 | + type round_half = string | :up | :down | :even | 'up' | 'down' | 'even' |
| 167 | + |
| 168 | + # Interface which indicates a type implements ruby's coercion scheme. |
| 169 | + # |
| 170 | + # The `coerce` method is only used in the stdlib of Ruby when an operator (such as `Integer#+`) |
| 171 | + # doesn't know how to handle the second argument. Instead, `other_arg.coerce(the_integer)` is |
| 172 | + # called, and then operation is then run on the two return arguments (`equiv_self + equiv_other`). |
| 173 | + # |
| 174 | + # Traditionally, `_Coerce` functions return a tuple of `[MyClass.new(...), self]`, this is not |
| 175 | + # a strict requirement, and there are cases where it's useful to return other types. As such, |
| 176 | + # three generics are required to represent `_Coerce`. |
| 177 | + interface _Coerce[Other, EquivOther, EquivSelf] |
| 178 | + # Coerce `other` and `self` into types where the original operation can be performed. |
| 179 | + # |
| 180 | + # See `_Coerce` for more details. |
| 181 | + def coerce: (Other other) -> [EquivOther, EquivSelf] |
| 182 | + end |
| 183 | + |
| 184 | + # Adds an `Other` to `self`, returning a `Return`. |
| 185 | + interface _OpAdd[Other, Return] |
| 186 | + # Performs the addition. |
| 187 | + def +: (Other other) -> Return |
| 188 | + end |
| 189 | + |
| 190 | + # Subtracts an `Other` from `self`, returning a `Return`. |
| 191 | + interface _OpSub[Other, Return] |
| 192 | + # Performs the subtraction. |
| 193 | + def -: (Other other) -> Return |
| 194 | + end |
| 195 | + |
| 196 | + # Multiplies `self` by an `Other`, returning a `Return`. |
| 197 | + interface _OpMul[Other, Return] |
| 198 | + # Performs the multiplication. |
| 199 | + def *: (Other other) -> Return |
| 200 | + end |
| 201 | + |
| 202 | + # Divides `self` by an `Other`, returning a `Return`. |
| 203 | + interface _OpDiv[Other, Return] |
| 204 | + # Performs the division. |
| 205 | + def /: (Other other) -> Return |
| 206 | + end |
| 207 | + |
| 208 | + # Modulos `self` by an `Other`, returning a `Return`. |
| 209 | + interface _OpMod[Other, Return] |
| 210 | + # Performs the modulus. |
| 211 | + def %: (Other other) -> Return |
| 212 | + end |
| 213 | + |
| 214 | + # Exponentiates `self` by `Other`, returning a `Return`. |
| 215 | + interface _OpPow[Other, Return] |
| 216 | + # Performs the exponentiation. |
| 217 | + def **: (Other other) -> Return |
| 218 | + end |
| 219 | + |
| 220 | + # Performs bitwise AND of `self` and `Other`, returning a `Return`. |
| 221 | + interface _OpAnd[Other, Return] |
| 222 | + # Performs the bitwise AND. |
| 223 | + def &: (Other other) -> Return |
| 224 | + end |
| 225 | + |
| 226 | + # Performs bitwise OR of `self` and `Other`, returning a `Return`. |
| 227 | + interface _OpOr[Other, Return] |
| 228 | + # Performs the bitwise OR. |
| 229 | + def |: (Other other) -> Return |
| 230 | + end |
| 231 | + |
| 232 | + # Performs bitwise XOR of `self` and `Other`, returning a `Return`. |
| 233 | + interface _OpXor[Other, Return] |
| 234 | + # Performs the bitwise XOR. |
| 235 | + def ^: (Other other) -> Return |
| 236 | + end |
| 237 | + |
| 238 | + # Returns whether `self` is less than `Other`. |
| 239 | + interface _OpLt[Other] |
| 240 | + # Performs the lesser-than comparison. |
| 241 | + def <: (Other other) -> bool |
| 242 | + end |
| 243 | + |
| 244 | + # Returns whether `self` is less than or equal to `Other`. |
| 245 | + interface _OpLe[Other] |
| 246 | + # Performs the lesser-than-or-equal-to comparison. |
| 247 | + def <=: (Other other) -> bool |
| 248 | + end |
| 249 | + |
| 250 | + # Returns whether `self` is greater than `Other`. |
| 251 | + interface _OpGt[Other] |
| 252 | + # Performs the greater-than comparison. |
| 253 | + def >: (Other other) -> bool |
| 254 | + end |
| 255 | + |
| 256 | + # Returns whether `self` is greater than or equal to `Other`. |
| 257 | + interface _OpGe[Other] |
| 258 | + # Performs the greater-than-or-equal-to comparison. |
| 259 | + def >=: (Other other) -> bool |
| 260 | + end |
| 261 | + |
| 262 | + # Performs integer division of `self` by `Other`. |
| 263 | + interface _Div[Other] |
| 264 | + # Performs the integer division. |
| 265 | + def div: (Other other) -> Integer |
| 266 | + end |
| 267 | + |
| 268 | + # Performs integer division of `self` by `Other`, returning the whole part and the `Remainder`. |
| 269 | + interface _Divmod[Other, Remainder] |
| 270 | + # Performs the division and modulus. |
| 271 | + def divmod: (Other other) -> [Integer, Remainder] |
| 272 | + end |
| 273 | + |
165 | 274 | public
|
166 | 275 |
|
167 | 276 | # <!--
|
|
0 commit comments