<div dir="ltr">Thanks for all the feedback everyone, I just submitted a short proposal as a PR: <a href="https://github.com/apple/swift-evolution/pull/782">https://github.com/apple/swift-evolution/pull/782</a></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 12, 2018 at 11:34 PM, Anders Kierulf via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I also avoid using ! for negation when possible, and `toggle` or `invert` will be helpful, but in many cases I think the negative case is better expressed directly. For example, I find that using `nonEmpty` instead of !isEmpty makes the code easier to read:<br>
<br>
extension String {<br>
var nonEmpty: Bool { return !self.isEmpty }<br>
}<br>
<br>
if !string.isEmpty { … }<br>
<br>
if string.isEmpty.inverted() { … }<br>
<br>
if string.nonEmpty { … }<br>
<br>
For the case of `contains`, maybe define `lacks`?<br>
<span class=""><br>
if !items.contains(item) { ... }<br>
<br>
if items.contains(item).inverted(<wbr>) { ... }<br>
<br>
</span> if items.lacks(item) { ... }<br>
<br>
Anders Kierulf<br>
<div class="HOEnZb"><div class="h5"><br>
> On Jan 12, 2018, at 12:54 PM, Alejandro Martinez via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
><br>
> I wouldn't go as far as to ask to fade out ! but in all my code I end<br>
> up doing == false just for readability. That ! knows who to hide<br>
> himself too well :P<br>
><br>
> On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution<br>
> <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>> I’m not sure if this would be considered or not, but I would like if the<br>
>> negation operator `!` would fade out.<br>
>><br>
>> If this is ever going to a review then I’d suggest that we add a pair of<br>
>> functions, one mutating and the other non-mutating.<br>
>><br>
>> extension Bool {<br>
>> mutating func invert() {<br>
>> self = !self<br>
>> }<br>
>><br>
>> func inverted() {<br>
>> return !self<br>
>> }<br>
>> }<br>
>><br>
>> I’d rather use `inverted` instead of `!` because of the readability this<br>
>> function provides.<br>
>><br>
>> if !items.contains(item) { ... }<br>
>><br>
>> if items.contains(item).inverted(<wbr>) { ... }<br>
>><br>
>> ——<br>
>><br>
>> I personally have some other extensions like:<br>
>><br>
>> extension Bool {<br>
>> @discardableResult<br>
>> func whenTrue<T>(execute closure: () throws -> T) rethrows -> T? {<br>
>> if self { return try closure() }<br>
>> return nil<br>
>> }<br>
>><br>
>> @discardableResult<br>
>> func whenFalse<T>(execute closure: () throws -> T) rethrows -> T? {<br>
>> if !self { return try closure() }<br>
>> return nil<br>
>> }<br>
>> }<br>
>><br>
>> But this is more a personal preference.<br>
>><br>
>> ——<br>
>><br>
>> That said, if the community is fine with the `invert/inverted` pair then I’d<br>
>> say go for it ;)<br>
>><br>
>> Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution<br>
>> (<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>) schrieb:<br>
>><br>
>><br>
>> On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution<br>
>> <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>><br>
>> Hey SE!<br>
>><br>
>> When we have a bunch of nested structs:<br>
>><br>
>> struct Sample {<br>
>> var bar: Bar<br>
>> }<br>
>><br>
>> struct Bar {<br>
>> var show: Bool<br>
>> }<br>
>><br>
>> var foo = Sample(bar: Bar(show: false))<br>
>><br>
>> It can be repetitive to toggle a deeply nested boolean:<br>
>><br>
>> foo.bar.show = !foo.bar.show // duplication<br>
>><br>
>> I sometimes add a `toggle` extension on `Bool`<br>
>><br>
>> extension Bool {<br>
>> mutating func toggle() {<br>
>> self = !self<br>
>> }<br>
>> }<br>
>><br>
>> This allows you to write the same code without duplication, and makes the<br>
>> intent clearer:<br>
>><br>
>> foo.bar.show.toggle()<br>
>><br>
>><br>
>> I like it!<br>
>><br>
>> In other languages, I don't think the `toggle` would make as much sense, but<br>
>> the mutable self makes this very useful.<br>
>><br>
>> After I posted it on Twitter, it turns out I'm not the only one:<br>
>> <a href="https://twitter.com/PublicExtension/status/730434956376346624" rel="noreferrer" target="_blank">https://twitter.com/<wbr>PublicExtension/status/<wbr>730434956376346624</a><br>
>><br>
>> I would have gone straight to a proposal, but I think we can do some<br>
>> bikeshedding about the name of `toggle`?<br>
>><br>
>><br>
>> Another verb that could work is `invert`.<br>
>><br>
>> The `!` operator that does this is the negation operator, but I think<br>
>> `negate` could sound to some like "make this false" rather than toggling.<br>
>><br>
>> Nate<br>
>> ______________________________<wbr>_________________<br>
>> swift-evolution mailing list<br>
>> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
>><br>
>><br>
>> ______________________________<wbr>_________________<br>
>> swift-evolution mailing list<br>
>> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
>><br>
><br>
><br>
><br>
> --<br>
> Alejandro Martinez<br>
> <a href="http://alejandromp.com" rel="noreferrer" target="_blank">http://alejandromp.com</a><br>
> ______________________________<wbr>_________________<br>
> swift-evolution mailing list<br>
> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br>
______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Chris Eidhof</div>
</div>