I asked a question: How would I write this in idiomatic Kotlin?
and now I had an idea for short this idiom. like below
private fun getTouchX(): Int = when(arguments)
containsKey(KEY_DOWN_X) -> getInt(KEY_DOWN_X)
else -> centerX()
}
containsKey
and getInt
are arguments
's method.
Of course this is not correct idiom for when
.
is there any possible way to do this?
arguments is Bundle class in Android framework. you can see at below https://developer.android.com/reference/android/os/Bundle.html https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/Bundle.java https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/BaseBundle.java
From the information you've provided I can only give you this answer:
private fun getTouchX(): Int = arguments.run {
if (containsKey(KEY_DOWN_X)) getInt(KEY_DOWN_X)
else centerX()
}
If arguments
is nullable, it can be like this:
private fun getTouchX(): Int = arguments?.run {
if (containsKey(KEY_DOWN_X)) getInt(KEY_DOWN_X)
else null
} ?: centerX()