除了How to write a basic swap function in Java中描述的java方式之外,还有什么更好的方法用kotlin编写泛型交换函数吗?
Is there any kotlin language feature which can make generic swap function more concise and intuitive?
除了How to write a basic swap function in Java中描述的java方式之外,还有什么更好的方法用kotlin编写泛型交换函数吗?
Is there any kotlin language feature which can make generic swap function more concise and intuitive?
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