Hi all, i get an unchecked cast warning on this co...
# announcements
u
Hi all, i get an unchecked cast warning on this code:
Copy code
typealias Factory<T> = () -> T

    private fun Bundle.getViewModelFactory(): (() -> VIEWMODEL)? {
        val serializable = getSerializable(ARG_VIEWMODEL_FACTORY)
        return serializable as? Factory<VIEWMODEL>
    }
Shouldn't
as?
take care of this?
c
as? still gives the warning for some reason, think that's a bug
d
At runtime, the type parameter cannot be checked I think. That's what the warning is about.
u
Thanks. That is it.
c
oh yeah, that makes sense. it'll check if it's a Function0, but not if it's Function0<VIEWMODEL>. that's fair
e
If you turn the getViewModelFactory method into a reified function it should get rid of the warning because the type will be known at runtime
u
Did you try? I suppose reified makes the type known at compile time
d
Reified makes the type known at runtime
You can't check generic types using
is
or
as