Also generate contains for meaningless combinations of types to prevent mistakenly using contains from an Iterable.

#KT-6361
This commit is contained in:
Ilya Gorbunov
2015-07-11 00:46:52 +03:00
parent b7fbb60db4
commit 1cc94f92e2
3 changed files with 75 additions and 4 deletions
@@ -54,6 +54,7 @@ enum class PrimitiveType {
}
fun PrimitiveType.isIntegral(): Boolean = this in PrimitiveType.integralPrimitives
fun PrimitiveType.isNumeric(): Boolean = this in PrimitiveType.numericPrimitives
class GenericFunction(val signature: String, val keyword: String = "fun") : Comparable<GenericFunction> {
@@ -120,12 +120,22 @@ fun ranges(): List<GenericFunction> {
fun contains(rangeType: PrimitiveType, itemType: PrimitiveType) = f("contains(item: $itemType)") {
only(RangesOfPrimitives)
only(rangeType)
returns("Boolean")
doc { "Checks if the specified [item] belongs to this range." }
body { "return start <= item && item <= end" }
val meaningless = (rangeType.isNumeric() != itemType.isNumeric())
if (!meaningless) {
returns("Boolean")
doc { "Checks if the specified [item] belongs to this range." }
body { "return start <= item && item <= end" }
}
else {
returns("Nothing")
annotations("""deprecated("The 'contains' operation for a range of $rangeType and $itemType item is not supported and should not be used.")""")
body { """throw UnsupportedOperationException()""" }
}
}
templates addAll numericPermutations.filter { it.first != it.second }.map { contains(it.first, it.second) }
val allPermutations = (numericPrimitives + PrimitiveType.Char).let { it.flatMap { from -> it.map { to -> from to to } }}
templates addAll allPermutations.filter { it.first != it.second }.map { contains(it.first, it.second) }
return templates
}