I've tinkered around and have come up with a neat general helper function:
inline fun <T> (() -> T?).untilNull(action: (T) -> Unit) {
while (true) action(this() ?: break)
}
Which can be called like this:
::getObject.untilNull { /* do something with "it" */ }
You can of course don't use this helper function and just stay with the while
while(true){
val result = getObject() ?: break
// do something with "result"
}
Also another solution would be to create an inline lambda and then imediatly invoke that:
var result = null
while ({ result = getObject(); result }() != null){
// do something with "result"
}
This could probably be optimized if you'd "save" the lambda first:
var result = null
var assignment = { result = getObject(); result };
while (assignment() != null){
// do something with "result"
}