Philipp Mayer
10/05/2020, 11:49 AM.map { (payment, requestModel) -> steps.performRequest(payment, requestModel)
.filter { (_, result) -> result is ApiResult.Success }
.map { (payment, result) -> steps.update(payment, result) }
After performing the request, I want to filter out all results which were not of Type ApiResult.Success
Unfortunately the compiler does not allow this, since update()
is expecting ApiResult.Success
.
What did I miss there?
My only solution right now is to set the result
Parameter to ApiResult and add a require()
block in my update function, but I'm not really happy with that.gsala
10/05/2020, 11:49 AM.filterIsInstance<ApiResult.Success>()
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-is-instance.htmlPhilipp Mayer
10/05/2020, 11:52 AMPair<Payment, ApiResult>
, that method would also filter out the payments.Vampire
10/05/2020, 12:05 PMupdate
always requires success, why is it a parameter?Vampire
10/05/2020, 12:07 PMresult
but simply hard-coded success, as you filter before that only success payments are leftPhilipp Mayer
10/05/2020, 12:08 PMPhilipp Mayer
10/05/2020, 12:08 PMVampire
10/05/2020, 12:08 PMVampire
10/05/2020, 12:08 PMVampire
10/05/2020, 12:08 PMresult as Success
Philipp Mayer
10/05/2020, 12:10 PMPhilipp Mayer
10/05/2020, 12:11 PMPhilipp Mayer
10/05/2020, 12:14 PMif (result !is ApiResult.Success) return
muliyul
10/05/2020, 12:16 PMVampire
10/05/2020, 12:21 PMVampire
10/05/2020, 12:22 PM.map { (payment, requestModel) -> steps.performRequest(payment, requestModel)
.filter { (_, result) -> result is ApiResult.Success }
.map { (payment, result) -> steps.update(payment, result as ApiResult.Success) }
AS you filtered for only the pairs that have success there, it should always succeed properlyPhilipp Mayer
10/05/2020, 12:27 PMPhilipp Mayer
10/05/2020, 12:28 PMArian Stolwijk
10/05/2020, 12:34 PM.mapNotNull { it as? FooBar }
is usually nice tooVampire
10/05/2020, 12:34 PMVampire
10/05/2020, 12:35 PM