How can I do this easier with Kotlin?
if (translation.equals(TRANSLATION_X) ||
translation.equals(TRANSLATION_Y) ||
translation.equals(TRANSLATION_Z)
) {
return
} else {
translation = TRANSLATION_X
}
First, you can use the structural equality operator ==
, which is translated to the .equals(...)
calls automatically: translation == TRANSLATION_X
instead of translation.equals(TRANSLATION_X)
.
Then, you can use the when
statement:
when (translation) {
TRANSLATION_X, TRANSLATION_Y, TRANSLATION_Z -> return
else -> translation = TRANSLATION_X
}
Another alternative that may be more efficient than a when
expression is to use a Set
:
val options = setOf(TRANSLATION_X, TRANSLATION_Y, TRANSLATION_Z)
if (translation in options) return
else translation = TRANSLATION_X
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With