I'm using variable shadowing and I have a something like
val selectedEntryIds: LiveData<Set<Long>>
get() = _selectedProductIds
private val _selectedProductIds = MutableLiveData<MutableSet<Long>>(mutableSetOf())
However I get an error saying type mismatch.
CodePudding user response:
Use the variance annotation out
like this:
val selectedEntryIds: LiveData<out Set<Long>>
get() = _selectedProductIds
To tell the compiler that the LiveData will only produce such a set. Check:
https://kotlinlang.org/docs/generics.html#declaration-site-variance
CodePudding user response:
You may be able to do the following
val selectedEntryIds: LiveData<Set<Long>>
get() = _selectedProductIds as LiveData<Set<Long>>
private val _selectedProductIds = MutableLiveData<MutableSet<Long>>(mutableSetOf())
as to cast the MutableLiveData<..>
into a regular LiveData<..>
object.