If you want to make this a bit βextreme,β you can define an extension function on Pair<String?,Int?> That hides the logic for you:
fun Pair<String?,Int?>.test(block: (String, Int) -> Unit) { if(first != null && second != null) { block(first, second) } }
then his call will be a little shorter
(name to age).test { n, a -> println("name: $n age: $a") }
However, this will not help you (since you could also define this as a function within the Person class itself) if you do not need such functionality very often throughout the project. As I said, this seems redundant.
change you could make it (a little) more useful by going all the way to the generic:
fun <T,R> Pair<T?,R?>.ifBothNotNull(block: (T, R) -> Unit) { if(first != null && second != null){ block(first, second) } }
Lovis source share