skip icon indicating copy to clipboard operation
skip copied to clipboard

Transpile `Bool.toggle()`

Open dfabulich opened this issue 7 months ago • 1 comments

https://developer.apple.com/documentation/swift/bool/toggle()

It's quite handy, and every time it comes up in my transpiled Swift project, I stub my toe and replace enabled.toggle() with enabled = !enabled.

This seems like it should be pretty straightforward to transpile.

dfabulich avatar May 04 '25 18:05 dfabulich

This would certainly be nice to have. The limitation is that we cannot extend pre-existing types (i.e., Bool) with a mutating function, because the way we handle mutating is to have the type implement our own value type handling code. It is the same reason we cannot implement String.append.

In theory we could identify these common mutating functions on a case-by-case basis and provide implementations at the call site. E.g.,

var b = true
b.toggle()

could be transpiled into the Kotlin:

var b = true
b = !b

marcprux avatar May 04 '25 22:05 marcprux