<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><br></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><br></div></div>-- <br><div class="gmail_signature">Chris Eidhof</div>
</div>