is there any better way to write generic swap function in kotlin other than java way described in How to write a basic swap function in Java.
Is there any kotlin language feature which can make generic swap function more concise and intuitive?
“Swap” can be used to drop one class and simultaneously add another. By swapping rather than dropping and then adding, you ensure that you will not give up your spot in one class and be unable to enroll in the other.
If your input is an array, you can use the Collections. swap() function to swap two elements in it. Since the Collections. swap() function accepts a list, you can get a fixed-size list “backed” by the array.
Syntax. constexpr void swap( T2 (&a)[N], T2 (&b)[N]); // Swaps the arrays a and b.
No need a swap function in Kotlin at all. you can use the existing also function, for example:
var a = 1 var b = 2 a = b.also { b = a } println(a) // print 2 println(b) // print 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With