reactormonk
09/20/2022, 9:47 AMparseResult
demands a non-null return value - however, null
is a possible result I may get in there. How do I wrap the null
into a non-null value, so the compiler is happy with me?Juliane Lehmann
09/20/2022, 10:00 AMdata class CreateFooResult(val foo: Foo?)
But more idiomatic would be to make the semantics of what a null
result means a bit clearer, e.g. like
sealed class CreateFooResult {
object Aborted : CreateFooResult()
data class Success(val foo: Foo) : CreateFooResult()
}
or whatever your situation is. You might even find out that you'd really like to distinguish between e.g. resultCode == Activity.RESULT_CANCEL
and resultCode == Activity.RESULT_OK && intent?.data == null
or somesuch (when one means that the user aborted the process and the other that there's some other reason)Robert Williams
09/20/2022, 10:05 AMO
is a nullable type (but using a descriptive result type will be clearer)Rob Elliot
09/20/2022, 10:49 AMparseResult
as @NotNull
. So whatever is calling it in the android system is going to fail nastily if he returns null there.mkrussel
09/20/2022, 11:55 AMreactormonk
09/20/2022, 12:09 PMreactormonk
09/20/2022, 12:14 PMNothing
isn't a valid type here?
class PickCompanionDevice<T: Parcelable> : ActivityResultContract<IntentSender, CompanionDeviceResult<T>>() {
override fun createIntent(context: Context, input: IntentSender): Intent {
val req = IntentSenderRequest.Builder(input).build()
return ActivityResultContracts.StartIntentSenderForResult().createIntent(context, req)
}
override fun parseResult(resultCode: Int, intent: Intent?): CompanionDeviceResult<T> {
val res = ActivityResultContracts.StartIntentSenderForResult().parseResult(resultCode, intent)
val device = res.data?.getParcelableExtra<T>(CompanionDeviceManager.EXTRA_DEVICE)
return if (device == null) {
CompanionDeviceResult.NothingSelected
} else {
CompanionDeviceResult.Selected(device)
}
}
}
sealed class CompanionDeviceResult<T> {
object NothingSelected: CompanionDeviceResult<Nothing>()
data class Selected<T>(val device: T): CompanionDeviceResult<T>()
}
Complains about
Type mismatch.
Required:
CompanionDeviceResult<T>
Found:
CompanionDeviceResult.NothingSelected
reactormonk
09/20/2022, 12:16 PMmkrussel
09/20/2022, 12:17 PMreactormonk
09/20/2022, 12:17 PMout
, no need for the Any
part though.