jw
01/03/2019, 3:12 AMuser
05/30/2021, 12:27 PMuser
05/31/2021, 1:52 PMAlexander Suraphel
05/31/2021, 5:07 PMuser
06/01/2021, 10:03 AMuser
06/01/2021, 10:12 AMuser
06/01/2021, 12:13 PMuser
06/01/2021, 3:00 PMPeter
06/01/2021, 4:02 PMCaused by: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Couldn't inline method call 'also' into
Peter
06/01/2021, 4:03 PMuser
06/02/2021, 1:58 PMTianyu Zhu
06/02/2021, 4:03 PMKProperty
as a key in a Map
?
• Is KProperty
created compile-time? or is it created by reflection at runtime?
• Will this cause memory leaks?
For context I've defined the following:
abstract class Mixin<Value> {
private val mixins = mutableMapOf<KProperty<*>, Value>()
companion object Property {
operator fun <Value, T: Value> getValue(mixin: Mixin<Value>, property: KProperty<*>): T {
return when (val value = mixin.mixins[property]) {
null -> throw NoSuchElementException("Uninitialized mix-in: ${property.name}")
else -> @Suppress("UNCHECKED_CAST") (value as T)
}
}
operator fun <Value> setValue(mixin: Mixin<Value>, property: KProperty<*>, value: Value) {
mixin.mixins[property] = value
}
}
}
The idea is that, by extending this class, I get to make type-safe property extensions on classes that extend `Mixin`:
// A dynamically extensible class declared in some parent module
class Shipment(id: String): Mixin<Any>
// Now, let's say I want to do track shipment status in some downstream module.
var Shipment.statusHistory: List<ShipmentStatus> by Mixin // declare the mixin
// Load the status history of the shipment from the database
fun Shipment.loadStatusHistory() {
statusHistory = statusDatabase.lookup(id)
}
// Check if the shipment is complete
val Shipment.isComplete: Boolean get() = statusHistory.any { it.state == ShipmentStatus.State.COMPLETE }
user
06/02/2021, 4:06 PMTony
06/02/2021, 5:24 PMuser
06/03/2021, 9:00 AMuser
06/03/2021, 11:35 AMuser
06/04/2021, 5:15 PMuser
06/05/2021, 5:22 PMuser
06/06/2021, 12:34 PMJolan Rensen [JetBrains]
06/07/2021, 3:13 PMArray<T>
makes each value be wrapped inside an Object in the Array, making it slower, so this is not what I wanted. So, I made value class wrappers around each type of primitive array which in turn inherit from one common sealed interface: PrimitiveArray
.
Let me know what you think on the forum: https://discuss.kotlinlang.org/t/poc-library-generic-primitive-arrays/22031user
06/07/2021, 3:30 PMuser
06/07/2021, 4:43 PMuser
06/07/2021, 6:01 PMuser
06/08/2021, 9:18 AMmagnumrocha
06/08/2021, 11:03 AMuser
06/08/2021, 2:38 PMViet Nguyen Tran
06/08/2021, 3:49 PMViet Nguyen Tran
06/08/2021, 3:49 PM