maxmello
08/10/2022, 1:38 PMpublic inline constructor(size: Int, init: (Int) -> T)
via reflection, but I don’t know how that second parameter is supposed to work.ephemient
08/10/2022, 3:04 PMinline
functions with reified type parameters via reflection, and Array
is even more special, being handled specifically by the compilerephemient
08/10/2022, 3:10 PMjava.lang.reflect.Array.newInstance(T::class.java, size) as Array<T>
if you can't use Array<T>(size) {...}
directlymaxmello
08/10/2022, 3:11 PMilya.gorbunov
08/15/2022, 4:53 PMHow do I create an Array<T> from a List<T>?if the type T is known at compile time, you can just use
list.toTypedArray()
ephemient
08/16/2022, 4:04 AMT
then that works, but so would the Array()
"constructor" so I assumed Max didn't have that available for some reasonmaxmello
08/16/2022, 11:57 AMif(prop.returnClass.java.isArray) {
val foundGenericType = prop.returnType.arguments.firstOrNull()?.type
val genericClass = foundGenericType?.classifier as? KClass<*>
val listVal = (prop as? KProperty1<T, Array<*>?>)?.get(this)?.toList()
val outputList = // Operate on list instead of array to generate output for reasons
val array = java.lang.reflect.Array.newInstance(genericClass?.java, outputList.size) as Array<Any?>
outputList.forEachIndexed { index, any ->
array[index] = any
}
return array
}
So I have a KClass, I would love to be able to do list.toTypedArray<genericClass::asTypeParameter>() 😄