Is there a proposal for a `+-` or `.plusOrMinus` o...
# language-proposals
x
Is there a proposal for a
+-
or
.plusOrMinus
operator that does
Copy code
val fruits = listOf("🍎", "🍊", "🍌")
print(fruits Β± "πŸ₯") // "🍎", "🍊", "🍌", "πŸ₯"
print(fruits ± "🍊") // "🍎", "🍌"
which works like
Copy code
operator fun <T> Collection<T>.plusOrMinus(item: T): Collection<T> = 
  if (item in this) this + item 
  else this - item
e
imo this should be
xor
if we ever got bitwise operators
x
Here's a simple usecase i have in my compose app where you can collapse sections within a list
Copy code
@Composable
fun ListScreen(items: List<Item>) {
  var collapsed: Set<Category> by rememberSaveable { mutableStateOf(emptySet()) }

  val groups = items.groupBy { item.category }

  LazyColumn {
    Category.entries.forEach { category ->
      item(span = FullLine) {
        CollapsableHeader(
          isCollapsed = category in collapsed,
          onClick = { collapsed = (collapsed Β± category).toSet() }
        ) {
          Text(category.text)
        }
      }
      
      if(category !in groups) items(groups[category].entries) { item ->
        ItemView(item)
      }
    }
  }
}
e
also it might be more similar to other collections extensions with a signature like
Copy code
infix fun <T> Iterable<T>.xor(item: T): List<T> = toMutableList().apply { remove(item) || add(item) }
x
does xor have a operator?
e
there's a youtrack request for bitwise operators but for now there's only infix funs
a
note that infix functions can take you a lot way
πŸ‘€ 1