https://kotlinlang.org logo
Title
u

uli

06/02/2019, 1:06 PM
Hi all, i get an unchecked cast warning on this 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

cypher121

06/02/2019, 2:54 PM
as? still gives the warning for some reason, think that's a bug
d

Dico

06/02/2019, 5:33 PM
At runtime, the type parameter cannot be checked I think. That's what the warning is about.
u

uli

06/02/2019, 9:55 PM
Thanks. That is it.
c

cypher121

06/03/2019, 12:11 AM
oh yeah, that makes sense. it'll check if it's a Function0, but not if it's Function0<VIEWMODEL>. that's fair
e

Evan R.

06/03/2019, 4:23 PM
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

uli

06/05/2019, 1:24 AM
Did you try? I suppose reified makes the type known at compile time
d

Dico

06/05/2019, 1:58 AM
Reified makes the type known at runtime
You can't check generic types using
is
or
as