Philip Dukhov
05/14/2026, 3:31 PMStateKeeper.consume function.
That's how my current variant works and builds fine:
inline fun <reified T> StateKeeper.test1() {
val value: T? = consume("key", serializer())
}
However, I now need to pass a custom serializer, so I tried updating the function as follows:
inline fun <reified T> StateKeeper.test2(
serializer: KSerializer<T> = serializer(),
) {
val value: T? = consume("key", serializer)
}
However, I now have a build issue related to the consume function having the T : Any restriction.
If I add the same restriction to my function, I won't be able to use it with optional types, even though the old version works fine with them and serializes properly.
Am I missing some knowledge about generic usage? Is the T : Any restriction really needed for consume?Arkadii Ivanov
05/14/2026, 11:13 PMKSerializer is able to deserialize non-nullable values.
I don't remember details why it was made T : Any. But you just don't need a nullable serializer, you can use non-nullable and it handles nulls internally anyway. Keep in mind that the return type of the consume function must be nullable anyway, because there might be no saved value.
E.g. it doesn't make sense to be able to write both of the following:
val s1: String? = stateKeeper.consume("key", serializer<String?>())
stateKeeper.register("key", serializer<String?>()) { null }
val s2: String? = stateKeeper.consume("key", serializer<String>())
stateKeeper.register("key", serializer<String>()) { null }
I will think about it and maybe allow T in the next major release, but unlikely.