Generate to[Primitive]Array() methods for generic arrays and collections.

#KT-4180 Fixed
This commit is contained in:
Ilya Gorbunov
2015-04-20 23:15:21 +03:00
parent d18b086113
commit 7fba979372
4 changed files with 231 additions and 0 deletions
@@ -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
"""
}
}
}