Generate sum() methods for arrays, iterables and sequences of Byte and Short. These overloads got explicit platformName.
Heuristics to decide when to set platformName. #KT-3714
This commit is contained in:
@@ -23,16 +23,10 @@ fun generateCollectionsAPI(outDir: File) {
|
||||
|
||||
numeric().writeTo(File(outDir, "_Numeric.kt")) {
|
||||
val builder = StringBuilder()
|
||||
// TODO: decide if sum for byte and short is needed and how to make it work
|
||||
for (numeric in listOf(PrimitiveType.Int, PrimitiveType.Long, /*Byte, Short, */ PrimitiveType.Double, PrimitiveType.Float)) {
|
||||
build(builder, Iterables, numeric)
|
||||
build(builder, Sequences, numeric)
|
||||
}
|
||||
for (numeric in numericPrimitives)
|
||||
for (family in buildFamilies)
|
||||
build(builder, family, numeric)
|
||||
|
||||
for (numeric in numericPrimitives) {
|
||||
build(builder, ArraysOfObjects, numeric)
|
||||
build(builder, ArraysOfPrimitives, numeric)
|
||||
}
|
||||
builder.toString()
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ fun List<GenericFunction>.writeTo(file: File, builder: GenericFunction.() -> Str
|
||||
its.use {
|
||||
its.append("package kotlin\n\n")
|
||||
its.append("$COMMON_AUTOGENERATED_WARNING\n\n")
|
||||
its.append("import kotlin.platform.*\n")
|
||||
its.append("import java.util.*\n\n")
|
||||
its.append("import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js\n\n")
|
||||
for (t in this.sort()) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.ArrayList
|
||||
import java.util.HashMap
|
||||
import java.util.HashSet
|
||||
import java.util.StringTokenizer
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
enum class Family {
|
||||
Sequences
|
||||
@@ -20,6 +21,8 @@ enum class Family {
|
||||
ProgressionsOfPrimitives
|
||||
Primitives
|
||||
Generic
|
||||
|
||||
val isPrimitiveSpecialization: Boolean by Delegates.lazy { this in listOf(ArraysOfPrimitives, RangesOfPrimitives, ProgressionsOfPrimitives, Primitives) }
|
||||
}
|
||||
|
||||
enum class PrimitiveType(val name: String) {
|
||||
@@ -169,7 +172,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") : Comp
|
||||
}
|
||||
|
||||
fun build(builder: StringBuilder, f: Family) {
|
||||
if (f == ArraysOfPrimitives || f == RangesOfPrimitives || f == ProgressionsOfPrimitives || f == Primitives) {
|
||||
if (f.isPrimitiveSpecialization) {
|
||||
for (primitive in buildPrimitives.sortBy { it.name() })
|
||||
build(builder, f, primitive)
|
||||
} else {
|
||||
@@ -308,6 +311,11 @@ class GenericFunction(val signature: String, val keyword: String = "fun") : Comp
|
||||
}
|
||||
}
|
||||
|
||||
fun getPlatformName(primitive: PrimitiveType): String {
|
||||
val name = signature.substringBefore('(').trim()
|
||||
return "${name}Of${primitive.name}"
|
||||
}
|
||||
|
||||
val methodDoc = docs[f] ?: doc
|
||||
if (methodDoc != "") {
|
||||
builder.append("/**\n")
|
||||
@@ -324,6 +332,10 @@ class GenericFunction(val signature: String, val keyword: String = "fun") : Comp
|
||||
builder.append("deprecated(\"$deprecated\")\n")
|
||||
}
|
||||
|
||||
// heuristics to define that platform overloads are likely to clash
|
||||
if (!f.isPrimitiveSpecialization && primitive != null && ((returnType == returnType.renderType() && returnType != "T") || returnType == "SUM"))
|
||||
builder.append("platformName(\"${getPlatformName(primitive!!)}\")\n")
|
||||
|
||||
builder.append("public ")
|
||||
if (inlineFamilies[f] ?: defaultInline)
|
||||
builder.append("inline ")
|
||||
|
||||
@@ -6,7 +6,8 @@ fun numeric(): List<GenericFunction> {
|
||||
val templates = arrayListOf<GenericFunction>()
|
||||
|
||||
templates add f("sum()") {
|
||||
doc { "Returns the sum of all elements in the collection" }
|
||||
exclude(Strings)
|
||||
doc { "Returns the sum of all elements in the collection." }
|
||||
returns("SUM")
|
||||
body {
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user