Kotlin 2.3 question. I have error in next line ```...
# compiler
e
Kotlin 2.3 question. I have error in next line
Copy code
messageActionList =
            requireArguments().getSerializableCompat(MESSAGE_ACTION_LIST) ?: emptyList() 
// Unresolved reference. None of the following candidates is applicable because of a receiver type mismatch:
fun <reified T : Serializable> Bundle.getSerializableCompat(key: String): T?
Where
messageActionList
type is
List<MessageAction>
and
MessageAction
is
Serializable
. How to fix compilation error?
Looks like List is not
Serializable
? Or how to understand it
The function is defined like
Copy code
inline fun <reified T : Serializable> Bundle.getSerializableCompat(key: String): T? =
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        getSerializable(key, T::class.java)
    } else {
        getSerializable(key) as? T
    }
d
Check what
requireArguments()
returns. The errors says about incompatible receiver type, not argument types.
e
requireArguments() : Bundle
d
Could you create a self-contained reproducer?
e
@dmitriy.novozhilov do you have any android project? Would be just kotlin playground enough or you need a github repo with project?
d
.zip
achieve with a gradle project would be the ideal.
e
I've sent in DM
d
Copy code
bundle.getSerializableCompat<List<Test>>("Test")
This one reports
Type argument is not within its bounds: must be subtype of 'Serializable'.
on the
List<Test>
type argument which is completely fair, as
List
is not inheritor of
Serializable
Moreover, the same exact code fails with the same error with 2.2.20. I suspect that the actual reason why your original code stopped to compile is because the inference of
?:
could slightly change. So previously it was inferred into something serializable and now it's just a
List
. Could you please tell me what is the type of the
messageActionList
property from the original example?
e
private lateinit var messageActionList: List<MessageAction>
Copy code
class MessageAction :
    Action(),
    Serializable
Copy code
open class Action(...): Serializable
Interesting that we are on kotlin 2.2.21 and it compiles
And project that I've sent to you indeed breaks on 2.2.21
d
private lateinit var messageActionList: List<MessageAction>
It's a still list, so there shouldn't be much difference. Maybe there is some other
getSerializableCompat
function in the project which doesn't have an upper bound?
e
I can not find it (at least in our source)
But TIL, I changed type to
ArrayList
and it started compiling
👍 1
I had impression that List is Serializable in Java. I was wrong.
Thank you!
d
No problem.