<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><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><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></body></html>