Benjamin Szilagyi
03/07/2024, 7:32 PMephemient
03/07/2024, 7:53 PMnot()
Benjamin Szilagyi
03/07/2024, 10:12 PMvanshg
03/07/2024, 11:46 PMvanshg
03/07/2024, 11:47 PMmutating func
. afaik this is not achievable in kotlinArun Sudharsan
03/20/2024, 7:21 AMBoolean
within an extension function because it's a value type and is passed by value, not by reference.
However, you can achieve a similar effect by using a wrapper class that holds the Boolean value. Here's how you can do it:
class MutableBoolean(var value: Boolean)
fun MutableBoolean.toggle(): Boolean {
this.value = !this.value
return this.value
}
You can use this toggle
function like this:
val myBool = MutableBoolean(true)
println(myBool.toggle()) // prints: false
println(myBool.toggle()) // prints: true