<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 <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> 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 <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> 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> struct Sample {</div><div> var bar: Bar</div><div> }</div><div> </div><div> struct Bar {</div><div> var show: Bool</div><div> }</div><div> </div><div> 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> 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> extension Bool {</div><div> mutating func toggle() {</div><div> self = !self</div><div> }</div><div> }</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> 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. </div><div><br></div><div><span style="background-color: rgba(255, 255, 255, 0);">“The <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">bitwise NOT operator</em> (<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. </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. </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. </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. </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>