nickk
05/23/2019, 4:39 PMdata class DataDriver<T>(val valueCalculator: (T) -> Int)
val dataDriver = listOf(
DataDriver<T1>( ::calcValue1),
DataDriver<T2>( ::calcValue2)
)
// Using reified worked here
private inline fun <reified T> query(isSubmitted: Boolean): List<T> {
return realm.where<T>().equalTo("submitted", isSubmitted).find()
}
fun calcValue1(item: T1) -> Int {
return 42
}
// But what about here?
// Apply transforms to get a list of calculated items,
// calling the proper query and calculator based on the type.
// Without resorting to type checks for each type.
// IS THIS POSSIBLE?
dataDriver.map {
query() // type inference fails here
.map(::valueCalculator)
}karelpeeters
05/23/2019, 4:48 PMnickk
05/23/2019, 4:50 PMkarelpeeters
05/23/2019, 4:51 PMwhere must use reflection behind the scenes anyway to do something with the reified type, so it's not going to be slower.karelpeeters
05/23/2019, 4:52 PMwhere is from)diesieben07
05/24/2019, 1:33 PMribesg
05/24/2019, 1:35 PMreified everywhere you want List will never retain any typenickk
05/24/2019, 1:41 PMdataDriver.map {
query() // type inference fails here
.map(::valueCalculator)
}
Could I solve this by restricting the type in query()?diesieben07
05/24/2019, 1:41 PMnickk
05/24/2019, 1:43 PMribesg
05/24/2019, 1:43 PMquery would be the type of the list, it’s simplediesieben07
05/24/2019, 1:44 PMquery that can accept e.g. a KClass instead of a reified type parameter.nickk
05/24/2019, 1:48 PMmap would not work…diesieben07
05/24/2019, 1:51 PMnickk
05/24/2019, 1:54 PMdataDriver.map {
query() // type inference fails here
.map(::valueCalculator)
}
query() is a generic function here.ribesg
05/24/2019, 2:05 PM.filterIsInstance<X>()diesieben07
05/24/2019, 2:18 PMdataDriver.map { query(it::class) }diesieben07
05/24/2019, 2:19 PMquery to accept KClass instead of a reified type parameternickk
05/24/2019, 2:40 PM