Antonio Acuña Prieto
05/25/2022, 6:55 PMfun delete(id: UUID) = list.removeIf { it -> it.id == id }
And I want to:
• If the element was removed, don’t return nothing.
• If the element was not found, return exception
For now I have something like:
fun delete(id: UUID) = takeIf { list.removeIf { it -> it.id == id } } ?: throw NotFoundException(id)
But this still returns a true.Vampire
05/25/2022, 7:07 PMfun delete(id: UUID) = if (list.removeIf { it -> it.id == id }) Unit else throw NotFoundException(id)
Antonio Acuña Prieto
05/25/2022, 7:10 PMEndre Deak
05/25/2022, 7:12 PMfun delete(id: UUID) { takeIf { list.removeIf { it -> it.id == id } } ?: throw NotFoundException(id) }
phldavies
05/25/2022, 7:57 PMfun delete(id: UUID) {
if (!list.removeIf { it.id == id }) throw NotFoundException(id)
}
Not sure using takeIf
gains you anything here, nor does having both cases on the if to allow for using an expression-body function