Martin Nordholts
10/06/2019, 6:24 AMObserver
prefix in the second line below in order for SAM conversion to work, but not in the first line? The last parameter has identical declarations in these two cases.
MutableLiveData<String>().observeForever({ }) // No 'Observer' prefix necessary
MutableLiveData<String>().observe(this, Observer { }) // 'Observer' prefix necessary!
// Java declarations
public void observeForever(@NonNull Observer<? super T> observer) {
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
ilya.gorbunov
10/06/2019, 4:36 PMLifecycleOwner
is also a SAM interface, so we've got two overloads here: the original one, where both owner and observer are interfaces, and the synthetic one, where both owner and observer are functional types. Passing this
instance to owner
selects the origninal one, so you have to pass observer
not as a function, but as an interface either.Martin Nordholts
10/06/2019, 4:44 PMMutableLiveData
? I.e. that the compiler pretends that there is a version of observe
that takes proper Kotlin functions as arguments?ilya.gorbunov
10/06/2019, 4:51 PMobserve
function. For example like here:Martin Nordholts
10/06/2019, 4:51 PMilya.gorbunov
10/06/2019, 4:52 PMMartin Nordholts
10/06/2019, 4:53 PMilya.gorbunov
10/06/2019, 4:56 PM