Marshall
03/12/2023, 5:35 PMclass Reflect1() {
val prop1: Reflect2? = null
}
class Reflect2() {
val prop2: Double? = null
}
fun <T, R> accessProp(item: T, prop: KProperty1<T, R>): R {
return prop.get(item)
}
class Test() {
init {
val r = Reflect1()
accessProp(r.prop1, Reflect2::prop2)
}
}
Marshall
03/12/2023, 5:36 PMaccessProp(r.prop1, Reflect2::prop2 as KProperty1<Reflect2?, Double?>)
Marshall
03/12/2023, 5:43 PMMarshall
03/12/2023, 5:51 PMfun <T, R> accessProp(item: T?, prop: KProperty1<T, R>): R? {
return item?.let { prop.get(it) }
}
Have to use T? and R?Adam S
03/12/2023, 8:43 PMKProperty1<T, R>
with a lambda that accepts T and returns R
fun <T, R> accessProp(item: T, prop: (T) -> R): R {
return prop(item)
}
Adam S
03/12/2023, 8:44 PMval r = Reflect1()
val rProp1: Reflect2? = r.prop1
val prop2Accessor: (Reflect2) -> Double? = Reflect2::prop2
accessProp(rProp1, prop2Accessor)
to me this makes it clearer where the mismatch is.
rProp1
has a nullable type of Reflect2?
, but the lambda of Reflect2::prop2
only accepts the non-null Reflect
So the type T
in accessProp()
needs to be nullable, T?
, but Reflect2::prop2
only accepts a non-null type.
To work around it, you can just use a lambda:
val r = Reflect1()
accessProp(r.prop1) { reflect2: Reflect2? ->
reflect2?.prop2
}
Adam S
03/12/2023, 8:48 PMMarshall
03/13/2023, 5:54 PMval key = itemProperty().select(JiraIssue::getKeyProperty)
val summary = itemProperty().select(JiraIssue::getSummaryProperty)
val description = itemProperty().select(JiraIssue::getDescriptionProperty)
val url = itemProperty().select(JiraIssue::getUrlProperty)
val comments = itemProperty().select(JiraIssue::comments)
or for cases where I'm defining a list of properties
val testProperties = listOf (
ModelMutableProperty("Test Num", Test::num),
ModelMutableProperty("Title", Test::title),
ModelMutableProperty("Purpose", Test::purpose),
ModelMutableProperty("State", Test::attractionState),
ModelMutableProperty("Mode", Test::mode),
ModelMutableProperty("Material", Test::material)
)
Marshall
03/13/2023, 5:58 PMMarshall
03/13/2023, 5:59 PMBindings.select<ObservableList<JiraComment>>(itemProperty(), "comments")
Marshall
03/13/2023, 6:10 PMval key = itemProperty().select{ it?.keyProperty }