Another one with nullability Let’s assume I have ...
# announcements
p
Another one with nullability Let’s assume I have a function
fun doSomething(input: SomeClass<Concrete?>)
. Use case is:
Copy code
val localInstance: SomeClass<Concrete> = ...
doSomething(localInstance)
Should I naively cast local instance to
localInstance as SomeClass<Concrete?>
to use this method? Assume that I can’t change the method.
🧵 1
m
Can you change
SomeClass
? It works if you make its type parameter covariant.
class SomeClass<out T>
This makes
SomeClass<Concrete>
a subtype of
SomeClass<Concrete?>
.
p
No, it’s
LiveData
in this case.
d
Then this method call cannot be done. The method (
doSomething
) might be trying to put a
null
into your
SomeClass
. If you were to pass in a
SomeClass<Concrete>
that would cause problems.
You need to change either the class or the method.
p
And what if I’d be able to change the method?
m
I think you can do this:
Copy code
fun doSomething(input: SomeClass<out Concrete?>)
1