From 9759be5493394071e278c9f3330a3bf336dc034a Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 15 Apr 2015 23:14:46 +0300 Subject: [PATCH] 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 --- js/js.libraries/src/core/kotlin_special.kt | 1 + libraries/stdlib/src/generated/_Aggregates.kt | 1 + libraries/stdlib/src/generated/_Arrays.kt | 1 + .../stdlib/src/generated/_Comparables.kt | 1 + libraries/stdlib/src/generated/_Elements.kt | 1 + libraries/stdlib/src/generated/_Filtering.kt | 1 + libraries/stdlib/src/generated/_Generators.kt | 1 + libraries/stdlib/src/generated/_Guards.kt | 1 + libraries/stdlib/src/generated/_Mapping.kt | 1 + libraries/stdlib/src/generated/_Numeric.kt | 369 +++++++++++------- libraries/stdlib/src/generated/_Ordering.kt | 1 + libraries/stdlib/src/generated/_Ranges.kt | 1 + libraries/stdlib/src/generated/_Sequences.kt | 1 + libraries/stdlib/src/generated/_Sets.kt | 1 + libraries/stdlib/src/generated/_Snapshots.kt | 1 + libraries/stdlib/src/generated/_SpecialJVM.kt | 1 + libraries/stdlib/src/generated/_Strings.kt | 1 + .../src/generators/GenerateCollections.kt | 12 +- .../src/generators/GenerateStandardLib.kt | 1 + .../kotlin-stdlib-gen/src/templates/Engine.kt | 14 +- .../src/templates/Numeric.kt | 3 +- 21 files changed, 270 insertions(+), 145 deletions(-) diff --git a/js/js.libraries/src/core/kotlin_special.kt b/js/js.libraries/src/core/kotlin_special.kt index 5fb54cf8308..7ac986e0cd9 100644 --- a/js/js.libraries/src/core/kotlin_special.kt +++ b/js/js.libraries/src/core/kotlin_special.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Aggregates.kt b/libraries/stdlib/src/generated/_Aggregates.kt index 6b4808c7ad3..950ef6815bf 100644 --- a/libraries/stdlib/src/generated/_Aggregates.kt +++ b/libraries/stdlib/src/generated/_Aggregates.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 2957d6334c2..f808ced1395 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Comparables.kt b/libraries/stdlib/src/generated/_Comparables.kt index 50d0505092e..060d310fef1 100644 --- a/libraries/stdlib/src/generated/_Comparables.kt +++ b/libraries/stdlib/src/generated/_Comparables.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Elements.kt b/libraries/stdlib/src/generated/_Elements.kt index b136570093c..f70c09381f9 100644 --- a/libraries/stdlib/src/generated/_Elements.kt +++ b/libraries/stdlib/src/generated/_Elements.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Filtering.kt b/libraries/stdlib/src/generated/_Filtering.kt index e5d1188228b..c5b716a7dc9 100644 --- a/libraries/stdlib/src/generated/_Filtering.kt +++ b/libraries/stdlib/src/generated/_Filtering.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Generators.kt b/libraries/stdlib/src/generated/_Generators.kt index 8da43c6b394..f00eeeafb1e 100644 --- a/libraries/stdlib/src/generated/_Generators.kt +++ b/libraries/stdlib/src/generated/_Generators.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Guards.kt b/libraries/stdlib/src/generated/_Guards.kt index f1458441efb..ce5593be8eb 100644 --- a/libraries/stdlib/src/generated/_Guards.kt +++ b/libraries/stdlib/src/generated/_Guards.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Mapping.kt b/libraries/stdlib/src/generated/_Mapping.kt index 13bc8722ae8..a103a03e4cd 100644 --- a/libraries/stdlib/src/generated/_Mapping.kt +++ b/libraries/stdlib/src/generated/_Mapping.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Numeric.kt b/libraries/stdlib/src/generated/_Numeric.kt index e9040051864..be4631c6a35 100644 --- a/libraries/stdlib/src/generated/_Numeric.kt +++ b/libraries/stdlib/src/generated/_Numeric.kt @@ -5,13 +5,15 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfInt") public fun Iterable.sum(): Int { val iterator = iterator() var sum: Int = 0 @@ -22,8 +24,9 @@ public fun Iterable.sum(): Int { } /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfInt") public fun Sequence.sum(): Int { val iterator = iterator() var sum: Int = 0 @@ -36,8 +39,9 @@ public fun Sequence.sum(): Int { deprecated("Migrate to using Sequence and respective functions") /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfInt") public fun Stream.sum(): Int { val iterator = iterator() var sum: Int = 0 @@ -48,8 +52,34 @@ public fun Stream.sum(): Int { } /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfInt") +public fun Array.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun IntArray.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfLong") public fun Iterable.sum(): Long { val iterator = iterator() var sum: Long = 0 @@ -60,8 +90,9 @@ public fun Iterable.sum(): Long { } /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfLong") public fun Sequence.sum(): Long { val iterator = iterator() var sum: Long = 0 @@ -74,8 +105,9 @@ public fun Sequence.sum(): Long { deprecated("Migrate to using Sequence and respective functions") /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfLong") public fun Stream.sum(): Long { val iterator = iterator() var sum: Long = 0 @@ -86,8 +118,166 @@ public fun Stream.sum(): Long { } /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfLong") +public fun Array.sum(): Long { + val iterator = iterator() + var sum: Long = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun LongArray.sum(): Long { + val iterator = iterator() + var sum: Long = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfByte") +public fun Iterable.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfByte") +public fun Sequence.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + + +deprecated("Migrate to using Sequence and respective functions") +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfByte") +public fun Stream.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfByte") +public fun Array.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun ByteArray.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfShort") +public fun Iterable.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfShort") +public fun Sequence.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + + +deprecated("Migrate to using Sequence and respective functions") +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfShort") +public fun Stream.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfShort") +public fun Array.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun ShortArray.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfDouble") public fun Iterable.sum(): Double { val iterator = iterator() var sum: Double = 0.0 @@ -98,8 +288,9 @@ public fun Iterable.sum(): Double { } /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfDouble") public fun Sequence.sum(): Double { val iterator = iterator() var sum: Double = 0.0 @@ -112,8 +303,9 @@ public fun Sequence.sum(): Double { deprecated("Migrate to using Sequence and respective functions") /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfDouble") public fun Stream.sum(): Double { val iterator = iterator() var sum: Double = 0.0 @@ -124,8 +316,34 @@ public fun Stream.sum(): Double { } /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfDouble") +public fun Array.sum(): Double { + val iterator = iterator() + var sum: Double = 0.0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun DoubleArray.sum(): Double { + val iterator = iterator() + var sum: Double = 0.0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +platformName("sumOfFloat") public fun Iterable.sum(): Float { val iterator = iterator() var sum: Float = 0.0f @@ -136,8 +354,9 @@ public fun Iterable.sum(): Float { } /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfFloat") public fun Sequence.sum(): Float { val iterator = iterator() var sum: Float = 0.0f @@ -150,8 +369,9 @@ public fun Sequence.sum(): Float { deprecated("Migrate to using Sequence and respective functions") /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfFloat") public fun Stream.sum(): Float { val iterator = iterator() var sum: Float = 0.0f @@ -162,128 +382,9 @@ public fun Stream.sum(): Float { } /** - * Returns the sum of all elements in the collection - */ -public fun Array.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection - */ -public fun IntArray.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection - */ -public fun Array.sum(): Long { - val iterator = iterator() - var sum: Long = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection - */ -public fun LongArray.sum(): Long { - val iterator = iterator() - var sum: Long = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection - */ -public fun Array.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection - */ -public fun ByteArray.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection - */ -public fun Array.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection - */ -public fun ShortArray.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection - */ -public fun Array.sum(): Double { - val iterator = iterator() - var sum: Double = 0.0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection - */ -public fun DoubleArray.sum(): Double { - val iterator = iterator() - var sum: Double = 0.0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ +platformName("sumOfFloat") public fun Array.sum(): Float { val iterator = iterator() var sum: Float = 0.0f @@ -294,7 +395,7 @@ public fun Array.sum(): Float { } /** - * Returns the sum of all elements in the collection + * Returns the sum of all elements in the collection. */ public fun FloatArray.sum(): Float { val iterator = iterator() diff --git a/libraries/stdlib/src/generated/_Ordering.kt b/libraries/stdlib/src/generated/_Ordering.kt index fbcb9fdff12..e2bf5091a23 100644 --- a/libraries/stdlib/src/generated/_Ordering.kt +++ b/libraries/stdlib/src/generated/_Ordering.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Ranges.kt b/libraries/stdlib/src/generated/_Ranges.kt index 33c8b5a327d..ebc0b39be71 100644 --- a/libraries/stdlib/src/generated/_Ranges.kt +++ b/libraries/stdlib/src/generated/_Ranges.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index cc6aeb10df9..515aedebb3f 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Sets.kt b/libraries/stdlib/src/generated/_Sets.kt index 66cdb749788..7ef5e6323ed 100644 --- a/libraries/stdlib/src/generated/_Sets.kt +++ b/libraries/stdlib/src/generated/_Sets.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Snapshots.kt b/libraries/stdlib/src/generated/_Snapshots.kt index b762c2a8008..2a4777839fd 100644 --- a/libraries/stdlib/src/generated/_Snapshots.kt +++ b/libraries/stdlib/src/generated/_Snapshots.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_SpecialJVM.kt b/libraries/stdlib/src/generated/_SpecialJVM.kt index 670f6f836a3..b05f53ad0e4 100644 --- a/libraries/stdlib/src/generated/_SpecialJVM.kt +++ b/libraries/stdlib/src/generated/_SpecialJVM.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 3e1a9154c80..dc2ced5acea 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -5,6 +5,7 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt index 299a64d14c4..0ec2a06ba48 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt @@ -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() } diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index 99ae45e7497..603c0f64d4d 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -43,6 +43,7 @@ fun List.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()) { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt index cf8e60f69db..584ebd7e619 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt @@ -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 ") diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt index bf5d5b9e36d..4b6dc6ceec2 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt @@ -6,7 +6,8 @@ fun numeric(): List { val templates = arrayListOf() 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 { """