dave08
09/21/2023, 4:26 PMinterface Cacheable {
v This is an error... R needs to inherit from Any... but I'd like R to OPTIONALLY be nullable
suspend operator fun <R> invoke(name: String, type: KClass<R>, vararg params: Any, block: suspend () -> R): R =
cache(name, type, *params, block = block)
}
Where it's being used, R
could be nullable or not nullable so I can't just use R?
and <R: Any>
... what other options do I have?Joffrey
09/21/2023, 4:29 PMKClass<R & Any>
?dave08
09/21/2023, 4:31 PMsuspend inline fun <reified R> Cacheable.cache(
name: String,
vararg params: Any,
noinline block: suspend () -> R
): R =
cache(name, R::class, *params, block = block)
cache is defined the same as invoke...Joffrey
09/21/2023, 4:32 PMR::class
cannot work if R
is nullabledave08
09/21/2023, 4:33 PMsuspend fun <R> cache(name: String, type: KClass<R & Any>, vararg params: Any, block: suspend () -> R): R
?fun foo(): Something? = cache("name") { ... }
Joffrey
09/21/2023, 4:37 PMKClass
? Maybe the reified serializersModule.serializer<T>()
is more lenient with nullable types:
https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/serializer.htmldave08
09/22/2023, 6:04 AMJoffrey
09/26/2023, 11:54 AM