Deprecate Char-to-Number conversions in stdlib (JVM and JS)

- Int.toChar was left non-deprecated because the replacement is not intrinsic yet.
- Number.toChar was left non-deprecated because otherwise the deprecation propagates to the override, Int.toChar.

KT-23451
This commit is contained in:
Ilya Gorbunov
2021-04-06 15:36:44 +03:00
parent b976cd812a
commit b64b96eee6
59 changed files with 273 additions and 169 deletions
+12
View File
@@ -47,18 +47,30 @@ public class Char private constructor() : Comparable<Char> {
public operator fun rangeTo(other: Char): CharRange
/** Returns the value of this character as a `Byte`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toByte()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toByte(): Byte
/** Returns the value of this character as a `Char`. */
public fun toChar(): Char
/** Returns the value of this character as a `Short`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toShort()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toShort(): Short
/** Returns the value of this character as a `Int`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toInt(): Int
/** Returns the value of this character as a `Long`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toLong()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toLong(): Long
/** Returns the value of this character as a `Float`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toFloat()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toFloat(): Float
/** Returns the value of this character as a `Double`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toDouble()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toDouble(): Double
companion object {
+1
View File
@@ -43,6 +43,7 @@ public abstract class Number {
/**
* Returns the [Char] with the numeric value equal to this number, truncated to 16 bits if appropriate.
*/
// @Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
public abstract fun toChar(): Char
/**
+10
View File
@@ -201,6 +201,8 @@ public class Byte private constructor() : Number(), Comparable<Byte> {
* The least significant 8 bits of the resulting `Char` code are the same as the bits of this `Byte` value,
* whereas the most significant 8 bits are filled with the sign bit of this value.
*/
@Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public override fun toChar(): Char
/**
* Converts this [Byte] value to [Short].
@@ -442,6 +444,8 @@ public class Short private constructor() : Number(), Comparable<Short> {
* The resulting `Char` code is equal to this value reinterpreted as an unsigned number,
* i.e. it has the same binary representation as this `Short`.
*/
@Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public override fun toChar(): Char
/** Returns this value. */
public override fun toShort(): Short
@@ -982,6 +986,8 @@ public class Long private constructor() : Number(), Comparable<Long> {
*
* The resulting `Char` code is represented by the least significant 16 bits of this `Long` value.
*/
@Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public override fun toChar(): Char
/**
* Converts this [Long] value to [Short].
@@ -1225,6 +1231,8 @@ public class Float private constructor() : Number(), Comparable<Float> {
*
* The resulting `Char` value is equal to `this.toInt().toChar()`.
*/
@Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public override fun toChar(): Char
/**
* Converts this [Float] value to [Short].
@@ -1464,6 +1472,8 @@ public class Double private constructor() : Number(), Comparable<Double> {
*
* The resulting `Char` value is equal to `this.toInt().toChar()`.
*/
@Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public override fun toChar(): Char
/**
* Converts this [Double] value to [Short].
@@ -12,9 +12,9 @@ package kotlin.ranges
* @property step the number by which the value is incremented on each step.
*/
internal class CharProgressionIterator(first: Char, last: Char, val step: Int) : CharIterator() {
private val finalElement = last.toInt()
private val finalElement: Int = last.code
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
private var next = if (hasNext) first.toInt() else finalElement
private var next: Int = if (hasNext) first.code else finalElement
override fun hasNext(): Boolean = hasNext
@@ -36,9 +36,9 @@ internal class CharProgressionIterator(first: Char, last: Char, val step: Int) :
* @property step the number by which the value is incremented on each step.
*/
internal class IntProgressionIterator(first: Int, last: Int, val step: Int) : IntIterator() {
private val finalElement = last
private val finalElement: Int = last
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
private var next = if (hasNext) first else finalElement
private var next: Int = if (hasNext) first else finalElement
override fun hasNext(): Boolean = hasNext
@@ -60,9 +60,9 @@ internal class IntProgressionIterator(first: Int, last: Int, val step: Int) : In
* @property step the number by which the value is incremented on each step.
*/
internal class LongProgressionIterator(first: Long, last: Long, val step: Long) : LongIterator() {
private val finalElement = last
private val finalElement: Long = last
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
private var next = if (hasNext) first else finalElement
private var next: Long = if (hasNext) first else finalElement
override fun hasNext(): Boolean = hasNext
+4 -4
View File
@@ -32,7 +32,7 @@ public open class CharProgression
/**
* The last element in the progression.
*/
public val last: Char = getProgressionLastElement(start.toInt(), endInclusive.toInt(), step).toChar()
public val last: Char = getProgressionLastElement(start.code, endInclusive.code, step).toChar()
/**
* The step of the progression.
@@ -54,7 +54,7 @@ public open class CharProgression
first == other.first && last == other.last && step == other.step)
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * (31 * first.toInt() + last.toInt()) + step)
if (isEmpty()) -1 else (31 * (31 * first.code + last.code) + step)
override fun toString(): String = if (step > 0) "$first..$last step $step" else "$first downTo $last step ${-step}"
@@ -94,7 +94,7 @@ public open class IntProgression
/**
* The last element in the progression.
*/
public val last: Int = getProgressionLastElement(start.toInt(), endInclusive.toInt(), step).toInt()
public val last: Int = getProgressionLastElement(start, endInclusive, step)
/**
* The step of the progression.
@@ -156,7 +156,7 @@ public open class LongProgression
/**
* The last element in the progression.
*/
public val last: Long = getProgressionLastElement(start.toLong(), endInclusive.toLong(), step).toLong()
public val last: Long = getProgressionLastElement(start, endInclusive, step)
/**
* The step of the progression.
+1 -1
View File
@@ -28,7 +28,7 @@ public class CharRange(start: Char, endInclusive: Char) : CharProgression(start,
first == other.first && last == other.last)
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * first.toInt() + last.toInt())
if (isEmpty()) -1 else (31 * first.code + last.code)
override fun toString(): String = "$first..$last"