https://kotlinlang.org logo
#codereview
Title
# codereview
c

christophsturm

03/10/2021, 1:44 PM
in this code, is there a way to avoid creating IdHandler twice?
Copy code
internal data class EntityInfo<T:Any>(val idHandler: IDHandler, val classInfo: ClassInfo<T>) {
    constructor(kClass: KClass<T>) : this(Table(kClass), IDHandler(kClass), ClassInfo(kClass, IDHandler(kClass)))
}
i guess its not possible because the primary constructor call has to be the first instruction of the secondary constructor so I cannot extract a variable for IdHandler(kClass)
s

Shawn

03/10/2021, 2:58 PM
you’re probably better off with a factory function in the companion object tbh
c

christophsturm

03/10/2021, 3:37 PM
or a top level method named like a constructor
Copy code
fun <T:Any>EntityInfo(kClass: KClass<T>): EntityInfo<T> {
    val idHandler = IDHandler(kClass)
    return EntityInfo(Table(kClass), idHandler, ClassInfo(kClass, idHandler))
}
internal data class EntityInfo<T:Any>(val table:Table, val idHandler: IDHandler<T>, val classInfo: ClassInfo<T>)
4 Views