<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div><br></div><div><br>On Jan 12, 2018, at 12:14 AM, Nate Cook via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><br><div>On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div><div>Hey SE!</div><div><br></div><div>When we have a bunch of nested structs:</div><div><br></div><div>&nbsp; &nbsp; struct Sample {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; var bar: Bar</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; struct Bar {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; var show: Bool</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; var foo = Sample(bar: Bar(show: false))</div><div><br></div><div>It can be repetitive to toggle a deeply nested boolean:</div><div><br></div><div>&nbsp; &nbsp; foo.bar.show = !foo.bar.show // duplication</div><div><br></div><div>I sometimes add a `toggle` extension on `Bool`</div><div><br></div><div>&nbsp; &nbsp; extension Bool {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; mutating func toggle() {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self = !self</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div><br></div><div>This allows you to write the same code without duplication, and makes the intent clearer:</div><div><br></div><div>&nbsp; &nbsp; foo.bar.show.toggle()</div></div></div></div></blockquote><div><br></div>I like it!<div><br><blockquote type="cite"><div><div dir="ltr"><div><div>In other languages, I don't think the `toggle` would make as much sense, but the mutable self makes this very useful.</div><div><br></div><div>After I posted it on Twitter, it turns out I'm not the only one: <a href="https://twitter.com/PublicExtension/status/730434956376346624">https://twitter.com/PublicExtension/status/730434956376346624</a></div><div><br></div><div>I would have gone straight to a proposal, but I think we can do some bikeshedding about the name of `toggle`?</div></div></div></div></blockquote><div><br></div><div>Another verb that could work is `invert`.</div></div></div></blockquote><div><br></div><div>That’s a good one. Similar to bit flipping which Apple calls inverting.&nbsp;</div><div><br></div><div><span style="background-color: rgba(255, 255, 255, 0);">“The&nbsp;<em style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">bitwise NOT operator</em>&nbsp;(<code class="code-voice" style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-all; word-wrap: break-word;">~</code>) inverts all bits in a number:”</span></div><div><br></div>I was thinking `flip` for this function but then I actually don’t think this extension is a good idea in the standard library :(<div><br></div><div>Should we also have a mutating extension for the `NOT` operator? Probably no.&nbsp;</div><div><br></div><div>If people need it, they can add it and call it toggle, invert, flip, reverse, negate or what ever they want. &nbsp;</div><div><br></div><div>It’s a slippery slope because it makes me want to have something like `not()` added to the library. I don’t think it’s worth it.&nbsp;</div><div><br></div><div><br></div><div><br><blockquote type="cite"><div><div><div><br></div><div>The `!` operator that does this is the negation operator, but I think `negate` could sound to some like "make this false" rather than toggling.&nbsp;</div><div><br></div><div>Nate</div></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></body></html>