Rob Elliot
11/20/2022, 7:17 PMRob Elliot
11/20/2022, 7:19 PMRob Elliot
11/20/2022, 7:20 PMRob Elliot
11/20/2022, 7:22 PMcte
actually creates the fields on the anonymous object, which I imagine is impossible?Youssef Shoaib [MOD]
11/20/2022, 7:43 PMgetDialect(), "RESULT_OF_A_QUERY")
right?Rob Elliot
11/20/2022, 8:00 PMby
delegation to deduce the string from the property nameYoussef Shoaib [MOD]
11/20/2022, 8:03 PMRob Elliot
11/20/2022, 8:43 PMYoussef Shoaib [MOD]
11/20/2022, 9:07 PMimport kotlin.reflect.*
open class JooqTable(var name: String = "")
operator fun <T: JooqTable> T.provideDelegate(thisRef: Any?, prop: KProperty<*>): T {
this.name = prop.name
return this
}
operator fun <T: JooqTable> T.getValue(thisRef: Any?, prop: KProperty<*>): T {
return this
}
fun main() {
val MY_TABLE by object: JooqTable() {
val myField: Int = 42
}
println(MY_TABLE.name)
println(MY_TABLE.myField)
}
You can also make the setters for name and dialect internal if you're making this as a library.
Keep in mind that the generics on provideDelegate and getValue is required so that you can access fields of your local object.
But yeah this just works and allows you to arbitrarily make any changes to your tableRob Elliot
11/20/2022, 9:11 PM