One fairly concise option is to create the Pair and then filter it:
(a to b).takeIf{ a != null && b != null }
But this isn't very good: it'll sometimes create a Pair unnecessarily, and the result type will have the Pair params both nullable, even though you know they can't be.
You could write an extension function to make it simpler.
Otherwise, I don't think you can do better than:
if (a != null && b != null) a to b else null
which is slightly longer-winded but has better efficiency and stricter typing.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…