kevinmost
01/24/2017, 10:22 PM.takeIf { ... } makes me think it might be useful to have a .orElse { ... } or similar that is basically a function version of the Elvis operator. Would be useful if you're chaining more methods afterwards because with an Elvis, you'd have to wrap the expression from the very start up to after the Elvis in parens, which can get kind of messy
Kind of a contrived example, but:
val lastTransactionId = api.getUser("userId")
.takeIf { it.transactions.isNotEmpty() }
.orElse { error("user has no transactions") }
.transactions.last.id
vs
val lastTransactionId = (api.getUser("userId")
.takeIf { it.transactions.isNotEmpty() }
?: error("user has no transactions"))
.transactions.last.id
Thoughts?