Make two overloads of copyOf to preserve nullness when size doesn't change

This commit is contained in:
Ilya Ryzhenkov
2014-03-05 20:12:24 +04:00
committed by Andrey Breslav
parent 108fdc0097
commit ceb2aa57f9
2 changed files with 99 additions and 23 deletions
@@ -14,16 +14,29 @@ fun specialJVM(): List<GenericFunction> {
}
}
// TODO: when newLength is larger than size, we can get null items
templates add f("copyOf(newSize: Int = size)") {
templates add f("copyOf()") {
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns new array which is a copy of the riginal array" }
doc { "Returns new array which is a copy of the original array" }
returns("SELF")
body {
"return Arrays.copyOf(this, size)"
}
body(ArraysOfObjects) {
"return Arrays.copyOf(this, size) as Array<T>"
}
}
// This overload can cause nulls if array size is expanding, hence different return overload
templates add f("copyOf(newSize: Int)") {
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns new array which is a copy of the original array" }
returns("SELF")
body {
"return Arrays.copyOf(this, newSize)"
}
returns(ArraysOfObjects) { "Array<T?>" }
body(ArraysOfObjects) {
"return Arrays.copyOf(this, newSize) as Array<T>"
"return Arrays.copyOf(this, newSize) as Array<T?>"
}
}