I think this is related to this topic, but instead of passing the value through, use the value as receiver, if I’m right.
Ps. I think the way i would write your code is:
arguments?.takeIf{ it.containsKey(KEY_DOWN_X) }
?.getInt(KEY_DOWN_X)
?: centerX()
Or even better, create an extensionFunction like
getOrElse
inline fun <reified T> Bundle.getOrElse(
key: String,
compute: () -> T?
): T = (this[key] as? T)?: compute()
Note, the compute will also be called if T is the incorrect type.
The other solution is like you original did
inline fun <reified T> Bundle.getOrElse(
key: String,
compute: () -> T?
): T = takeIf{it.containsKey(key)}
?.let{this[key] as T)}
?: compute()
And then you throw an error by the incorrect type