miha-x64
10/10/2018, 1:49 PMpublic operator fun <T> Array<T>.plus(elements: Array<out T>): Array<T>
have out
receiver variance?
enum class A { A }
enum class B { B }
val sum = A.values() + B.values()
// adding the following function makes the code above work:
private inline operator fun <reified T> Array<out T>.plus(elements: Array<out T>): Array<T> {
val sum = arrayOfNulls<T>(size + elements.size)
System.arraycopy(this, 0, sum, 0, size)
System.arraycopy(elements, 0, sum, size, elements.size)
return sum as Array<T>
}
Dominaezzz
10/10/2018, 2:00 PMinline
?miha-x64
10/10/2018, 2:08 PMinline operator fun <reified T> Array<out T>.plus(elements: Array<out T>): Array<T> {
@Suppress("UNCHECKED_CAST") // make non-nullable
return copyContents(this, elements, arrayOfNulls<T>(size + elements.size)) as Array<T>
}
fun <T> copyContents(src1: Array<out T>, src2: Array<out T>, dest: Array<T>): Array<T> {
System.arraycopy(src1, 0, dest, 0, src1.size)
System.arraycopy(src2, 0, dest, src1.size, src2.size)
return dest
}
ilya.gorbunov
10/10/2018, 2:36 PMplus
defined this way (as inline reified) couldn't be used for array with non-reified generic element type.miha-x64
10/10/2018, 2:58 PMObject[]
under the hood, erasing arrays like generics? Only for interop?Dominaezzz
10/10/2018, 3:44 PMmiha-x64
10/10/2018, 4:48 PMArray<X>
will be X[]
under the hood. That's why array factories (Array<T>
, arrayOfNulls
) require reified type parameters.Dominaezzz
10/10/2018, 4:49 PMX
is known at runtime.Dominaezzz
10/10/2018, 4:50 PMX
is erased anyways.