Nice suggestion. I will try to make something that gives you a similar feature to Swift’s guards.
EDIT: Would this suite your needs?
inline fun assert(predicate: () -> Boolean) = assert(predicate())
infix fun assert(result: Boolean) = Guard(result)
class Guard(val result: Boolean) {
infix fun or(exception: Exception) {
if (result)
throw exception
}
infix fun or(body: () -> Any) {
if (result)
body()
}
}
Example usage:
assert (param > 0) or IllegalArgumentException("My message")
And:
assert (param is String) or {
... do stuff ...
}
// unfortunately Kotlin doesn't do smart cast here (because it would slow compilation)