suppose there are 2 classes:
class MyLiveData:LiveData<Int>()
class MyMutableLiveData:MutableLiveData<Int>()
Casting from MutableLiveData
to LiveData
is permitted:
val ld1=MutableLiveData<Int>()
val ld2:LiveData<Int> = ld1 //ok
But you can't cast your own implementations this way:
val mutable=MyMutableLiveData()
val immutable:MyLiveData = mutable //type missmatch
I understand that MutableLiveData extends LiveData thats why they are castable.But I can't have MyMutableLiveData
extending MyLiveData
as it won't be mutable in this case
Are there any workarounds?
UPD:I guess I need to show motivation of extending LiveData
.I'm trying to implement MutableLiveDataCollection
which notifies not just value changes via setValue/postValue
but also value modification like adding new elements.I'm surprised there is no native solution for this.
Anyway to obseve modify
events there have to be additional observe method.And this method have to be inside immutable part aka LiveDataCollection
because views will call it.Inheritance is natural solution here IMHO.