althaf
06/16/2021, 3:19 AMfun foo() : Response {
workFlowLog?.let { Response.Sucess(BatchApprovalWorkFlowList(workFlowLog) ) } ?: run { Response.Error(Exception()) }
}
My intention is to return different response in case workflow is null. My team mates say this kinda of ugly. Is there a better kotlin idiom to craft this statement.
if( w == null) ... else ... , we are trying to avoid this.ephemient
06/16/2021, 3:23 AMrun { }
, can't you?althaf
06/16/2021, 3:24 AMephemient
06/16/2021, 3:25 AMif
, or when
. go with whatever the team likes. nothing wrong with if
if it's cleareralthaf
06/16/2021, 3:25 AMRajkumar Singh
06/16/2021, 3:26 AMalthaf
06/16/2021, 3:27 AMRajkumar Singh
06/16/2021, 3:32 AMif
or when
would also be fine.Dave K
06/16/2021, 3:36 AMalthaf
06/16/2021, 3:38 AMalthaf
06/16/2021, 3:38 AMRajkumar Singh
06/16/2021, 3:41 AMDave K
06/16/2021, 3:42 AMephemient
06/16/2021, 4:27 AMinline fun <T : Any, R> T?.ifNotNull(orElse: () -> R = ::TODO, block: (T) -> R) = if (this == null) orElse() else block(this)
fun foo(): Response {
workFlowLog.ifNotNull(orElse = { Response.Error(Exception()) }) { Response.Success(BatchApprovalWorkFlowList(it)) }
}
but that doesn't strike me as an improvement