elect
06/12/2019, 9:22 AMdiesieben07
06/12/2019, 9:26 AMinline
+ reified
exactly?elect
06/12/2019, 9:27 AMclass SteamVR_RingBuffer<T>(clazz: Class<T>, size: Int) {
protected val buffer = Array(size) {
clazz.getDeclaredConstructor().newInstance() }
only functions can be reified
diesieben07
06/12/2019, 9:28 AMcompanion object {
inline operator fun <reified T : Any> invoke() = SteamVR_RingBuffer(T::class)
}
elect
06/12/2019, 9:28 AMdiesieben07
06/12/2019, 9:28 AMelect
06/12/2019, 9:29 AMT
here
class SteamVR_RingBuffer<T : Any>(val clazz: Class<T>, size: Int) {
val default: T
get() = clazz.getDeclaredConstructor().newInstance()
protected val buffer = Array(size) { default }
cant use T as reified parameter, use a class instead
clazz
eitherdiesieben07
06/12/2019, 9:39 AMArray<T>
.
Arrays are weird on the JVM in that Array<String>
and Array<Any>
are completely different classes (every element type has it's own class)Array<Any>
and insert appropriate casts at the appropriate places.ArrayList
do inside, too.elect
06/12/2019, 9:40 AMcbruegg
06/12/2019, 9:48 AMelect
06/12/2019, 9:48 AMcbruegg
06/12/2019, 9:49 AMSteamVRRingBuffer
elect
06/12/2019, 9:49 AMcbruegg
06/12/2019, 9:50 AMArrayList
is, as the name implies, backed by an array and should give similar performance.elect
06/12/2019, 9:50 AMcbruegg
06/12/2019, 9:50 AM