adams2
05/07/2020, 11:01 PMandroidx.lifecycle.observe
ktx extension. When I do viewModel.observe(this, ::myOnChanged)
, I get this error: Type mismatch: inferred type is KFunction1<Action, Unit?> but Observer<in Action!> was expected
. However, when I do viewModel.observe(this) { myOnChanged(it) }
, the extension functions as expected. Is there some precedence rule preventing me from using a method reference?Anas Tariq
05/08/2020, 6:08 AMval observer = Observer<MyObject> { myObject ->
myOnChanged(myObject)
}
viewModel.observe(this, observer)
Kit
05/08/2020, 3:21 PMKit
05/08/2020, 3:22 PMTravis
05/08/2020, 3:24 PMmyOnChanged
fun is probably written as an expression that returns a nullable like action?.let { /* do something */ }
so it's having trouble SAM converting to an Observer. Try either explicitly returning Unit from myOnChanged
or just defining the function without the expression since you're trying to return Unit anyway.adams2
05/11/2020, 4:19 PMUnit?
and not Unit