I'm not missing something basic here right. If I ...
# getting-started
c
I'm not missing something basic here right. If I have
Copy code
if (it.key.keyCode == 1234 && it.type == KeyEventType.KeyUp) {
and now I want to add a new code to check against I have to do
Copy code
if ((it.key.keyCode == 1234 || it.key.keyCode == 5678) && it.type == KeyEventType.KeyUp) {
right? No idiotmatic thing I can do here to make it more like this?
Copy code
if ((it.key.keyCode == 1234 || 5678) && it.type == KeyEventType.KeyUp) {
l
One option is to use a Set:
Copy code
if (setOf(1234, 5678).contains(it.key.keyCode) && it.type == KeyEventType.KeyUp) {
m
I think using
in
would be more common
Copy code
if (it.key.keyCode in setOf(1234, 5678) && it.type == KeyEventType.KeyUp)
But really just a preference.
l
You're probably right. I'm coming to Kotlin through Multiplatform so I don't know the most "Kotlin-ey" way to do things yet. 🙂
e
keyCode in intArrayOf(1234, 5678)
involves less boxing
âž• 4
if you want to avoid additional objects being constructed, there's no super nice syntax but you can write
Copy code
if (it.key.keyCode.let { keyCode -> keyCode == 1234 || keyCode == 5678) &&
    it.type == KeyEventType.KeyUp
) {
or
Copy code
if (
    when (it.key.keyCode) {
        1234, 5678 -> it.type == KeyEventType.KeyUp
        else -> false
    }
) {
or wait for https://github.com/Kotlin/KEEP/issues/371
c
I like the
in
approach! Seems more readable than my || statement. thanks for teaching!
m
You can also have a static array of allowed key codes to avoid creating objects each time. But the array should be short lived and easy to GC.
c
Similarly... I wrote this today
Copy code
if (screenState.manager == null || screenState.reviewInfo == null)
theres no way to just do like
Copy code
if ((screenState.manager || screenState.reviewInfo) == null)
right?
e
technically you could
Copy code
if (screenState.manager?.let { screenState.reviewInfo } != null)
but that's definitely not an improvement lol
c
lol
but okay. im not missing something basic here at least.
in my head. for whatever reason my brain thinks in this format
if ((screenState.manager || screenState.reviewInfo) == null)
and just wasn't sure if that was somehow valid but i just didn't know
e
go write in Raku then :P
Copy code
if ($foo | $bar).defined
👀 1