Generate to[Primitive]Array() methods for generic arrays and collections.
#KT-4180 Fixed
This commit is contained in:
@@ -21,6 +21,14 @@ fun generateCollectionsAPI(outDir: File) {
|
||||
specialJVM().writeTo(File(outDir, "_SpecialJVM.kt")) { build() }
|
||||
ranges().writeTo(File(outDir, "_Ranges.kt")) { build() }
|
||||
|
||||
toPrimitiveArrays().writeTo(File(outDir, "_ArraysToPrimitiveArrays.kt")) {
|
||||
val builder = StringBuilder()
|
||||
for (family in buildFamilies)
|
||||
build(builder, family, buildPrimitives.single())
|
||||
|
||||
builder.toString()
|
||||
}
|
||||
|
||||
numeric().writeTo(File(outDir, "_Numeric.kt")) {
|
||||
val builder = StringBuilder()
|
||||
for (numeric in numericPrimitives)
|
||||
|
||||
@@ -56,3 +56,34 @@ fun arrays(): List<GenericFunction> {
|
||||
|
||||
return templates
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun toPrimitiveArrays(): List<GenericFunction> =
|
||||
PrimitiveType.values().map { primitive ->
|
||||
val arrayType = primitive.name + "Array"
|
||||
f("to$arrayType()") {
|
||||
only(ArraysOfObjects, Collections)
|
||||
only(primitive)
|
||||
doc(ArraysOfObjects) { "Returns an array of ${primitive.name} containing all of the elements of this generic array." }
|
||||
doc(Collections) { "Returns an array of ${primitive.name} containing all of the elements of this collection." }
|
||||
returns(arrayType)
|
||||
body {
|
||||
"""
|
||||
val result = $arrayType(size())
|
||||
for (index in indices)
|
||||
result[index] = this[index]
|
||||
return result
|
||||
"""
|
||||
}
|
||||
body(Collections) {
|
||||
"""
|
||||
val result = $arrayType(size())
|
||||
var index = 0
|
||||
for (element in this)
|
||||
result[index++] = element
|
||||
return result
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user