https://kotlinlang.org logo
#rx
Title
# rx
i

iex

02/11/2020, 3:51 PM
another one 😅 it's not possible to instantiate
LiveData
with a fixed value (like `Observable`'s
just
), without having to use
MutableLiveData
?
k

kioba

02/11/2020, 4:06 PM
I am not sure about how
LiveData
works but based on the docs LiveData has a default value constructor. https://developer.android.com/reference/androidx/lifecycle/LiveData.html#LiveData(T) Is that what you are looking for?
i

iex

02/11/2020, 5:42 PM
that doesn't work, because
LiveData
is an abstract class @kioba
which actually answers the question
there's no non-mutable subclass apparently
so it's not possible
k

kioba

02/11/2020, 6:39 PM
sorry, I haven’t used LiveData but you might have more luck in the #android channel
m

Maciek

02/12/2020, 7:04 AM
@iex simplest factory method would solve your problem
Copy code
fun <T> LiveData(initValue: T): LiveData<T> = MutableLiveData<T>().apply { value = initValue }
i

iex

02/12/2020, 9:57 AM
@Maciek that's convenient but a hack kinda 😛
but yeah why not
would probably not capitalize the method's name though
m

Maciek

02/12/2020, 10:02 AM
to be honest capitalized letter is also kinda a pattern, at least in kotlin standard library 😉 here's the factory method for list from standard library
Copy code
public inline fun <T> List(size: Int, init: (index: Int) -> T): List<T> = MutableList(size, init)