Rob Elliot
12/16/2022, 10:04 AMval x by delegate
?
I could stick it in a cache in the Delegate class, but wondering if there's a simpler way... I suppose I could wrap it in a lazy, but I'd rather it was eagerly evaluated. I'm really only using it for syntax sugar to reduce boilerplate.Sam
12/16/2022, 10:06 AMRob Elliot
12/16/2022, 10:06 AMclass UserTable(dialect: SQLDialect) : DbTable(dialect, "USERS") {
val USER_ID = fieldFor("USER_ID", Long::class.java)
}
and I'm using a delegate to reduce that to:
class UserTable(dialect: SQLDialect) : DbTable(dialect, "USERS") {
val USER_ID by field<Long>()
}
Sam
12/16/2022, 10:07 AMRob Elliot
12/16/2022, 10:07 AMRob Elliot
12/16/2022, 10:08 AMfieldFor
method actually mutates the state of the class, so I'm getting a lot of fields called USER_ID
at the moment because everytime USER_ID
is called a new field is created.Sam
12/16/2022, 10:10 AMfield<Long>()
method should only be called once, when the property is created. What’s your implementation of that method?Rob Elliot
12/16/2022, 10:12 AMSam
12/16/2022, 10:12 AMSam
12/16/2022, 10:14 AMfield
, but you actually only get it inside the call to getValue
Sam
12/16/2022, 10:14 AMVampire
12/16/2022, 10:15 AMbut the property delegate only gives you the property name at the point when the property is accessed, not when it’s initialized.You can probably use
provideDelegate
to change thatVampire
12/16/2022, 10:16 AMSam
12/16/2022, 10:19 AMval USER_ID by field<Long>()
private inline fun <reified T> field() =
PropertyDelegateProvider<Any, ReadOnlyProperty<Any, T>> { _, property ->
val value = fieldFor(property.name, T::class.java)
ReadOnlyProperty { _,_ -> value }
}
Sam
12/16/2022, 10:19 AMRob Elliot
12/16/2022, 10:20 AMRob Elliot
12/16/2022, 10:27 AMRob Elliot
12/16/2022, 10:36 AM@PublishedApi internal fun
Rob Elliot
12/16/2022, 10:36 AM