https://kotlinlang.org logo
Title
p

pawel.urban

09/12/2018, 11:38 AM
Another one with nullability Let’s assume I have a function
fun doSomething(input: SomeClass<Concrete?>)
. Use case is:
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.
:thread-please: 1
m

marstran

09/12/2018, 11:43 AM
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

pawel.urban

09/12/2018, 11:50 AM
No, it’s
LiveData
in this case.
d

diesieben07

09/12/2018, 11:52 AM
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

pawel.urban

09/12/2018, 11:55 AM
And what if I’d be able to change the method?
m

marstran

09/12/2018, 11:56 AM
I think you can do this:
fun doSomething(input: SomeClass<out Concrete?>)
1