```if (data in selectedItems) { selectedItems....
# announcements
j
Copy code
if (data in selectedItems) {
    selectedItems.remove(data)
} else {
    selectedItems.add(data)
}
It is a better way to write this type of condition in Kotlin ?
selectedItems
is an arraylist and
data
is an object of arraylist type
s
Copy code
if(!selectedItems.remove(data)) {
    selectedItems.add(data)
}
j
If that works, I guess you have the option of
Copy code
selectedItems.remove(data) || selectedItems.add(data)
but personally I find your original structure the most readable, which I would consider more important
馃憤 2
s
and you could put that into an extension function
Copy code
fun <T> MutableCollection<T>.toogle(value: T) {
    remove(value) || add(value)
}
鉁旓笍 2
馃憤 2
t
Personally I wouldn鈥檛 let that last form through PR. It might work but the intent is just not clear
馃挴 4
j
Thank you all for your messages. My colleague started with something like
with(selectedItems) { if (data in this) ... else ... }
but I said "you do kotlin like code just for doing kotlin code 馃 . And I proposed to wrote it with a simple if else, i guess I was right, for me too it's the most readable, but I'm not a Kotlin expert for now, so I asked to see if a most simple/idiomatic/readable code was possible.