J茅r么me Gully
02/07/2020, 1:51 PMif (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 typeStephan Schroeder
02/07/2020, 2:07 PMif(!selectedItems.remove(data)) {
selectedItems.add(data)
}
Jonathan Mew
02/07/2020, 2:11 PMselectedItems.remove(data) || selectedItems.add(data)
but personally I find your original structure the most readable, which I would consider more importantStephan Schroeder
02/07/2020, 2:13 PMfun <T> MutableCollection<T>.toogle(value: T) {
remove(value) || add(value)
}
tddmonkey
02/07/2020, 2:36 PMJ茅r么me Gully
02/08/2020, 10:29 PMwith(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.