Deprecate Double, Float and Comparable range implementations.

This commit is contained in:
Ilya Gorbunov
2015-10-13 20:38:15 +03:00
parent aeb4abe39d
commit 69dc6701cc
15 changed files with 279 additions and 94 deletions
@@ -47,10 +47,30 @@ fun comparables(): List<GenericFunction> {
}
}
templates add f("coerceIn(range: Range<T>)") {
sourceFile(SourceFile.Ranges)
only(Primitives)
only(numericPrimitives.filter { it.isIntegral() })
returns("SELF")
doc {
"""
Ensures that this value lies in the specified [range].
@return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end.
"""
}
body {
"""
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.")
return if (this < range.start) range.start else if (this > range.end) range.end else this
"""
}
}
templates add f("coerceIn(range: Range<T>)") {
sourceFile(SourceFile.Ranges)
only(Primitives, Generic)
only(numericPrimitives)
only(numericPrimitives.filterNot { it.isIntegral() })
returns("SELF")
typeParam("T: Comparable<T>")
doc {
@@ -60,6 +80,7 @@ fun comparables(): List<GenericFunction> {
@return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end.
"""
}
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.ERROR))
body {
"""
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.")
@@ -8,7 +8,7 @@ fun ranges(): List<GenericFunction> {
templates add f("reversed()") {
only(RangesOfPrimitives, ProgressionsOfPrimitives)
exclude(PrimitiveType.Boolean)
only(PrimitiveType.integralPrimitives)
doc(ProgressionsOfPrimitives) { "Returns a progression that goes over the same range in the opposite direction with the same step." }
doc(RangesOfPrimitives) { "Returns a progression that goes over this range in reverse direction." }
returns("TProgression")
@@ -20,11 +20,27 @@ fun ranges(): List<GenericFunction> {
}
}
templates add f("reversed()") {
only(RangesOfPrimitives, ProgressionsOfPrimitives)
only(defaultPrimitives - PrimitiveType.Boolean - PrimitiveType.integralPrimitives)
doc(ProgressionsOfPrimitives) { "Returns a progression that goes over the same range in the opposite direction with the same step." }
doc(RangesOfPrimitives) { "Returns a progression that goes over this range in reverse direction." }
returns("TProgression")
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.ERROR))
annotations("""@Suppress("DEPRECATION_ERROR")""")
body(RangesOfPrimitives) {
"return TProgression(end, start, -ONE)"
}
body(ProgressionsOfPrimitives) {
"return TProgression(end, start, -increment)"
}
}
templates add f("step(step: SUM)") {
infix(true)
only(RangesOfPrimitives, ProgressionsOfPrimitives)
exclude(PrimitiveType.Boolean)
only(PrimitiveType.integralPrimitives)
doc(ProgressionsOfPrimitives) { "Returns a progression that goes over the same range with the given step." }
doc(RangesOfPrimitives) { "Returns a progression that goes over this range with given step." }
returns("TProgression")
@@ -34,6 +50,30 @@ fun ranges(): List<GenericFunction> {
return TProgression(start, end, step)
"""
}
body(ProgressionsOfPrimitives) {
"""
checkStepIsPositive(step > 0, step)
return TProgression(start, end, if (increment > 0) step else -step)
"""
}
}
templates add f("step(step: SUM)") {
infix(true)
only(RangesOfPrimitives, ProgressionsOfPrimitives)
only(defaultPrimitives - PrimitiveType.Boolean - PrimitiveType.integralPrimitives)
doc(ProgressionsOfPrimitives) { "Returns a progression that goes over the same range with the given step." }
doc(RangesOfPrimitives) { "Returns a progression that goes over this range with given step." }
returns("TProgression")
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.ERROR))
annotations("""@Suppress("DEPRECATION_ERROR")""")
body(RangesOfPrimitives) {
"""
checkStepIsPositive(step > 0, step)
return TProgression(start, end, step)
"""
}
bodyForTypes(RangesOfPrimitives, PrimitiveType.Float, PrimitiveType.Double) {
"""
if (step.isNaN()) throw IllegalArgumentException("Step must not be NaN.")
@@ -66,6 +106,11 @@ fun ranges(): List<GenericFunction> {
"""
}
if (!fromType.isIntegral() || !toType.isIntegral()) {
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.ERROR))
annotations("""@Suppress("DEPRECATION_ERROR")""")
}
val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
val toExpr = if (elementType == toType) "to" else "to.to$elementType()"
val incrementExpr = when (elementType) {
@@ -101,6 +146,11 @@ fun ranges(): List<GenericFunction> {
"""
}
if (!fromType.isIntegral() || !toType.isIntegral()) {
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.ERROR))
annotations("""@Suppress("DEPRECATION_ERROR")""")
}
val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
if (elementType == toType) {
@@ -129,7 +179,12 @@ fun ranges(): List<GenericFunction> {
fun contains(rangeType: PrimitiveType, itemType: PrimitiveType) = f("contains(item: $itemType)") {
operator(true)
assert(rangeType.isNumeric() != itemType.isNumeric())
if (!rangeType.isIntegral()) {
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.ERROR))
annotations("""@Suppress("DEPRECATION_ERROR")""")
}
check(rangeType.isNumeric() == itemType.isNumeric()) { "Require rangeType and itemType both be numeric or both not, got: $rangeType, $itemType" }
only(Ranges)
onlyPrimitives(Ranges, rangeType)
platformName("${rangeType.name.decapitalize()}RangeContains")