# \[Idea\] swap expression

**URL:** <https://discuss.kotlinlang.org/t/idea-swap-expression/12965>\
**Category:** Language Design\
**Created:** [June 5, 2019, 10:45am UTC](https://discuss.kotlinlang.org/t/idea-swap-expression/12965 "2019-06-05T10:45:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wireless4024](https://avatars.discourse-cdn.com/v4/letter/w/ecae2f/32.png) [@wireless4024](https://discuss.kotlinlang.org/u/wireless4024)\
**Post date:** [June 5, 2019, 10:45am UTC](https://discuss.kotlinlang.org/t/idea-swap-expression/12965/1 "2019-06-05T10:45:20Z")

</div>

Hello.  
I have an idea like this

```auto
//i will use `obj` as example object
obj[a<-b] //i like this expression
or
obj[a->b]
or
obj[a:=b]

```

That will turn into

```auto
obj.swap(a,b)

```

`a` or `b` can be any type like this

```auto
obj[1 <- 2] into obj.swap(1,2)
obj["a" <- "b"] into obj.swap("a","b")

```

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 5, 2019, 10:50am UTC](https://discuss.kotlinlang.org/t/idea-swap-expression/12965/2 "2019-06-05T10:50:26Z")

</div>

I’m not really sure why this would be needed. In my experience the only place where you really need swap is if you implement low level data structures, search algorithms, etc.  
This is quite a small area of programming and mostly covered by the standard library already (or in case of jvm the java standard library) so I don’t think it would clear the [minus 100 point rule](https://blogs.msdn.microsoft.com/ericgu/2004/01/12/minus-100-points/) which many people here in this forum and elsewhere have used to judge an addition to the language.

Also I don’t like the syntax. It’s confusing. Imagine you have no idea what it is and have to guess. I don’t think adding a new construct to the language that isn’t used often and then also unintuitive is a good idea.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [June 5, 2019, 10:50am UTC](https://discuss.kotlinlang.org/t/idea-swap-expression/12965/3 "2019-06-05T10:50:46Z")

</div>

Do you really has a lot of collection element swaps in your program? Maybe you are doing something wrong?

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 5, 2019, 10:53am UTC](https://discuss.kotlinlang.org/t/idea-swap-expression/12965/4 "2019-06-05T10:53:04Z")

</div>

Also why would `obj[1 <- 2]` be better than `obj.swap(1, 2)`.  
Your version isn’t even shorter, it’s less expressive and most people would have to look up what it does when they see it for the first time. `obj.swap(1, 2)` on the other hand is expressive, about the same length and does not require a new language construct.

---

<div class="post-metadata">

**Author:** ![wireless4024](https://avatars.discourse-cdn.com/v4/letter/w/ecae2f/32.png) [@wireless4024](https://discuss.kotlinlang.org/u/wireless4024)\
**Post date:** [June 5, 2019, 10:54am UTC](https://discuss.kotlinlang.org/t/idea-swap-expression/12965/5 "2019-06-05T10:54:24Z")

</div>

Ok. It’s really bad idea 😂

---

<div class="post-metadata">

**Author:** ![Alexey.Belkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexey.belkov/32/5215_2.png) [@Alexey.Belkov](https://discuss.kotlinlang.org/u/Alexey.Belkov)\
**Post date:** [June 5, 2019, 12:37pm UTC](https://discuss.kotlinlang.org/t/idea-swap-expression/12965/6 "2019-06-05T12:37:10Z")

</div>

The swap use case is one of possible use cases of destructuring assignment, which is not implemented at the moment. See discussion here: [https://youtrack.jetbrains.com/issue/KT-11362#focus=streamItem-27-1846892.0-0](https://youtrack.jetbrains.com/issue/KT-11362#focus=streamItem-27-1846892.0-0)
