Skip to content
Draft
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
12 changes: 11 additions & 1 deletion library/src/scala/Boolean.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,15 @@ object Boolean extends AnyValCompanion {
/** The String representation of the scala.Boolean companion object. */
override def toString = "object scala.Boolean"

extension (self: Boolean) {

/** Compares `this` to `that` according to the standard total ordering.
*
* Returns:
* - a positive value if `this` is `true` and `that` is `false`
* - a negative value if `this` is `false` and `that` is `true`
* - `0` if `this == that`
*/
def compare(that: Boolean): Int = java.lang.Boolean.compare(self, that)
}
}

88 changes: 87 additions & 1 deletion library/src/scala/Byte.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package scala

import scala.language.`2.13`

import scala.collection.immutable.NumericRange

/** `Byte`, a 8-bit signed integer (equivalent to Java's `byte` primitive type) is a
* subtype of [[scala.AnyVal]]. Instances of `Byte` are not
* represented by an object in the underlying runtime system.
Expand Down Expand Up @@ -485,5 +487,89 @@ object Byte extends AnyValCompanion {
implicit def byte2long(x: Byte): Long = x.toLong
implicit def byte2float(x: Byte): Float = x.toFloat
implicit def byte2double(x: Byte): Double = x.toDouble
}

extension (self: Byte) {
/** Returns `'''true'''` if this number has no decimal component.
* Always `'''true'''` for `Byte`.
*/
@deprecated("isWhole on Byte is always true", "2.12.15")
def isWhole: Boolean = true
Comment on lines +495 to +496
Copy link
Member

Choose a reason for hiding this comment

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

I understand why this was done, but it feels weird to say that an extension method was deprecated since 2.12.15 😅


/** Returns `true` iff this is within the
* range of [[scala.Char]] MinValue and MaxValue; otherwise returns `false`.
*/
def isValidChar: Boolean = self >= 0

/** Returns `true` iff this is within the
* range of [[scala.Byte]] MinValue and MaxValue; otherwise returns `false`.
*/
@deprecated("isValidByte on Byte is always true", "3.8.0")
def isValidByte: Boolean = true

/** Returns `true` iff this is within the
* range of [[scala.Short]] MinValue and MaxValue; otherwise returns `false`.
*/
@deprecated("isValidShort on Byte is always true", "3.8.0")
def isValidShort: Boolean = true

/** Returns `true` iff this is within the
* range of [[scala.Int]] MinValue and MaxValue; otherwise returns `false`.
*/
@deprecated("isValidInt on Byte is always true", "3.8.0")
def isValidInt: Boolean = true

/** Returns the absolute value of `this`. */
def abs: Byte = java.lang.Math.abs(self.toInt).toByte

/** Returns `this` if `this > that` or `that` otherwise. */
def max(that: Byte): Byte = java.lang.Math.max(self.toInt, that.toInt).toByte

/** Returns `this` if `this < that` or `that` otherwise. */
def min(that: Byte): Byte = java.lang.Math.min(self.toInt, that.toInt).toByte
Comment on lines +525 to +528
Copy link
Member

Choose a reason for hiding this comment

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

(my personal preferences here)

Suggested change
def max(that: Byte): Byte = java.lang.Math.max(self.toInt, that.toInt).toByte
/** Returns `this` if `this < that` or `that` otherwise. */
def min(that: Byte): Byte = java.lang.Math.min(self.toInt, that.toInt).toByte
infix def max(that: Byte): Byte = java.lang.Math.max(self.toInt, that.toInt).toByte
/** Returns `this` if `this < that` or `that` otherwise. */
infix def min(that: Byte): Byte = java.lang.Math.min(self.toInt, that.toInt).toByte


/** Returns the sign of `this`.
*
* `0` if `this == 0`, `-1` if `this < 0` and `1` if `this > 0`.
*/
def sign: Byte = java.lang.Integer.signum(self.toInt).toByte

/** Returns the signum of `this`. */
@deprecated("use `sign` method instead", since = "2.13.0")
def signum: Int = self.sign.toInt

/** Compares `this` to `that` according to the standard total ordering.
*
* Returns:
* - a positive value if `this > that`
* - a negative value if `this < that`
* - `0` if `this == that`
*/
def compare(that: Byte): Int = java.lang.Byte.compare(self, that)

/** A [[scala.collection.immutable.NumericRange]] from `this` up to but not including `end`.
*
* @param end The final bound of the range to make.
*/
def until(end: Byte): NumericRange.Exclusive[Byte] = NumericRange(self, end, 1)
Copy link
Member

Choose a reason for hiding this comment

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

Since it is part of the very little subset where everyone agrees on it :). Same goes for to.

Suggested change
def until(end: Byte): NumericRange.Exclusive[Byte] = NumericRange(self, end, 1)
infix def until(end: Byte): NumericRange.Exclusive[Byte] = NumericRange(self, end, 1)

Copy link
Member Author

Choose a reason for hiding this comment

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

This PR is strictly a refactoring. Adding infix anywhere should be done separately.

Copy link
Member

Choose a reason for hiding this comment

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

If we view the PR this way, then okay.


/** A [[scala.collection.immutable.NumericRange]] from `this` up to but not including `end`.
*
* @param end The final bound of the range to make.
* @param step The number to increase by for each step of the range.
*/
def until(end: Byte, step: Byte): NumericRange.Exclusive[Byte] = NumericRange(self, end, step)

/** A [[scala.collection.immutable.NumericRange]] from `this` up to and including `end`.
*
* @param end The final bound of the range to make.
*/
def to(end: Byte): NumericRange.Inclusive[Byte] = NumericRange.inclusive(self, end, 1)

/** A [[scala.collection.immutable.NumericRange]] from `this` up to and including `end`.
*
* @param end The final bound of the range to make.
* @param step The number to increase by for each step of the range.
*/
def to(end: Byte, step: Byte): NumericRange.Inclusive[Byte] = NumericRange.inclusive(self, end, step)
}
}
121 changes: 120 additions & 1 deletion library/src/scala/Char.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package scala

import scala.language.`2.13`

import scala.collection.immutable.NumericRange

/** `Char`, a 16-bit unsigned integer (equivalent to Java's `char` primitive type) is a
* subtype of [[scala.AnyVal]]. Instances of `Char` are not
* represented by an object in the underlying runtime system.
Expand Down Expand Up @@ -484,5 +486,122 @@ object Char extends AnyValCompanion {
implicit def char2long(x: Char): Long = x.toLong
implicit def char2float(x: Char): Float = x.toFloat
implicit def char2double(x: Char): Double = x.toDouble
}

extension (self: Char) {

/** Returns `'''true'''` if this number has no decimal component.
* Always `'''true'''` for `RichInt`.
*/
@deprecated("isWhole on Char is always true", "2.12.15")
def isWhole: Boolean = true

/** Returns `true` iff this is within the
* range of [[scala.Char]] MinValue and MaxValue; otherwise returns `false`.
*/
@deprecated("isValidChar on Char is always true", "3.8.0")
def isValidChar: Boolean = true

/** Returns `true` iff this is within the
* range of [[scala.Byte]] MinValue and MaxValue; otherwise returns `false`.
*/
def isValidByte: Boolean = self.toInt <= Byte.MaxValue.toInt

/** Returns `true` iff this is within the
* range of [[scala.Short]] MinValue and MaxValue; otherwise returns `false`.
*/
def isValidShort: Boolean = self.toInt <= Short.MaxValue.toInt

/** Returns `true` iff this is within the
* range of [[scala.Int]] MinValue and MaxValue; otherwise returns `false`.
*/
@deprecated("isValidInt on Char is always true", "3.8.0")
def isValidInt: Boolean = true

/** Returns the absolute value of `this`. */
@deprecated("Char's are never negative; abs is redundant and can be removed", since = "3.8.0")
def abs: Char = self

/** Returns `this` if `this > that` or `that` otherwise. */
def max(that: Char): Char = java.lang.Math.max(self.toInt, that.toInt).toChar

/** Returns `this` if `this < that` or `that` otherwise. */
def min(that: Char): Char = java.lang.Math.min(self.toInt, that.toInt).toChar

/** Returns the sign of `this`.
*
* zero if the argument is zero, -zero if the argument is -zero,
* one if the argument is greater than zero, -one if the argument is less than zero,
* and NaN if the argument is NaN where applicable.
*/
@deprecated("since Char's are never negative, compare to '\\u0000' instead", since = "3.8.0")
def sign: Char = java.lang.Integer.signum(self.toInt).toChar

/** Returns the signum of `this`. */
@deprecated("use `sign` method instead", since = "2.13.0")
def signum: Int = self.sign

/** Compares `this` to `that` according to the standard total ordering.
*
* Returns:
* - a positive value if `this > that`
* - a negative value if `this < that`
* - `0` if `this == that`
*/
def compare(that: Char): Int = java.lang.Character.compare(self, that)

/** A [[scala.collection.immutable.NumericRange]] from `this` up to but not including `end`.
*
* @param end The final bound of the range to make.
*/
def until(end: Char): NumericRange.Exclusive[Char] = NumericRange(self, end, '\u0001')

/** A [[scala.collection.immutable.NumericRange]] from `this` up to but not including `end`.
*
* @param end The final bound of the range to make.
* @param step The number to increase by for each step of the range.
*/
def until(end: Char, step: Char): NumericRange.Exclusive[Char] = NumericRange(self, end, step)

/** A [[scala.collection.immutable.NumericRange]] from `this` up to and including `end`.
*
* @param end The final bound of the range to make.
*/
def to(end: Char): NumericRange.Inclusive[Char] = NumericRange.inclusive(self, end, '\u0001')

/** A [[scala.collection.immutable.NumericRange]] from `this` up to and including `end`.
*
* @param end The final bound of the range to make.
* @param step The number to increase by for each step of the range.
*/
def to(end: Char, step: Char): NumericRange.Inclusive[Char] = NumericRange.inclusive(self, end, step)

def asDigit: Int = Character.digit(self, Character.MAX_RADIX)

def isControl: Boolean = Character.isISOControl(self)
def isDigit: Boolean = Character.isDigit(self)
def isLetter: Boolean = Character.isLetter(self)
def isLetterOrDigit: Boolean = Character.isLetterOrDigit(self)
def isWhitespace: Boolean = Character.isWhitespace(self)
def isSpaceChar: Boolean = Character.isSpaceChar(self)
def isHighSurrogate: Boolean = Character.isHighSurrogate(self)
def isLowSurrogate: Boolean = Character.isLowSurrogate(self)
def isSurrogate: Boolean = isHighSurrogate || isLowSurrogate
def isUnicodeIdentifierStart: Boolean = Character.isUnicodeIdentifierStart(self)
def isUnicodeIdentifierPart: Boolean = Character.isUnicodeIdentifierPart(self)
def isIdentifierIgnorable: Boolean = Character.isIdentifierIgnorable(self)
def isMirrored: Boolean = Character.isMirrored(self)

def isLower: Boolean = Character.isLowerCase(self)
def isUpper: Boolean = Character.isUpperCase(self)
def isTitleCase: Boolean = Character.isTitleCase(self)

def toLower: Char = Character.toLowerCase(self)
def toUpper: Char = Character.toUpperCase(self)
def toTitleCase: Char = Character.toTitleCase(self)

def getType: Int = Character.getType(self)
def getNumericValue: Int = Character.getNumericValue(self)
def getDirectionality: Byte = Character.getDirectionality(self)
def reverseBytes: Char = Character.reverseBytes(self)
}
}
102 changes: 101 additions & 1 deletion library/src/scala/Double.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package scala

import scala.language.`2.13`

import scala.collection.immutable.NumericRange

/** `Double`, a 64-bit IEEE-754 floating point number (equivalent to Java's `double` primitive type) is a
* subtype of [[scala.AnyVal]]. Instances of `Double` are not
* represented by an object in the underlying runtime system.
Expand Down Expand Up @@ -253,5 +255,103 @@ object Double extends AnyValCompanion {

/** The String representation of the scala.Double companion object. */
override def toString = "object scala.Double"
}

extension (self: Double) {
/** Returns `'''true'''` if this number is finite and has no decimal component. */
def isWhole: Boolean = {
val l = self.toLong
l.toDouble == self || l == Long.MaxValue && self < Double.PositiveInfinity || l == Long.MinValue && self > Double.NegativeInfinity
}

/** Returns `true` iff this has a zero fractional part, and is within the
* range of [[scala.Char]] MinValue and MaxValue; otherwise returns `false`.
*/
def isValidChar: Boolean = self.toChar.toDouble == self

/** Returns `true` iff this has a zero fractional part, and is within the
* range of [[scala.Byte]] MinValue and MaxValue; otherwise returns `false`.
*/
def isValidByte: Boolean = self.toByte.toDouble == self

/** Returns `true` iff this has a zero fractional part, and is within the
* range of [[scala.Short]] MinValue and MaxValue; otherwise returns `false`.
*/
def isValidShort: Boolean = self.toShort.toDouble == self

/** Returns `true` iff this has a zero fractional part, and is within the
* range of [[scala.Int]] MinValue and MaxValue; otherwise returns `false`.
*/
def isValidInt: Boolean = self.toInt.toDouble == self

/** Returns `true` iff `this` is a `NaN` value. */
def isNaN: Boolean = java.lang.Double.isNaN(self)

/** Returns `true` iff `this` is `PositiveInfinity` or `NegativeInfinity`. */
def isInfinity: Boolean = java.lang.Double.isInfinite(self)

/** Returns `true` iff `this` is a finite value, i.e., not an infinity nor `NaN`. */
def isFinite: Boolean = java.lang.Double.isFinite(self)

/** Returns `true` iff `this` is `PositiveInfinity`. */
def isPosInfinity: Boolean = Double.PositiveInfinity == self

/** Returns `true` iff `this` is `NegativeInfinity`. */
def isNegInfinity: Boolean = Double.NegativeInfinity == self

/** Returns the absolute value of `this`. */
def abs: Double = java.lang.Math.abs(self)

/** Returns `this` if `this > that` or `that` otherwise. */
def max(that: Double): Double = java.lang.Math.max(self, that)

/** Returns `this` if `this < that` or `that` otherwise. */
def min(that: Double): Double = java.lang.Math.min(self, that)

/** Returns the sign of `this`.
*
* - `1.0` if `this > 0.0`;
* - `-1.0` if `this < 0.0`;
* - `this` otherwise (for zeros and `NaN`).
*/
def sign: Double = java.lang.Math.signum(self)

/** Returns the signum of `this`. */
@deprecated("signum does not handle -0.0 or Double.NaN; use `sign` method instead", since = "2.13.0")
def signum: Int = self.sign.toInt

/** Returns the closest `Long` to `this`. */
def round: Long = java.lang.Math.round(self)

/** Returns the smallest integer greater or equal to `this`. */
def ceil: Double = java.lang.Math.ceil(self)

/** Returns the largest integer smaller or equal to `this`. */
def floor: Double = java.lang.Math.floor(self)

/** Converts an angle measured in degrees to an approximately equivalent
* angle measured in radians.
*
* @return the measurement of the angle x in radians.
*/
def toRadians: Double = java.lang.Math.toRadians(self)

/** Converts an angle measured in radians to an approximately equivalent
* angle measured in degrees.
* @return the measurement of the angle x in degrees.
*/
def toDegrees: Double = java.lang.Math.toDegrees(self)

/** Compares `this` to `that` according to the standard total ordering.
*
* Returns:
* - a positive value if `this > that`
* - a negative value if `this < that`
* - `0` if `this == that`
*
* Special cases for this method:
* - `0.0` is considered greater than `-0.0`
* - `NaN` is considered greater than all other values, but equal to itself
*/
def compare(that: Double): Int = java.lang.Double.compare(self, that)
}
}
Loading
Loading