# Adding toggle to Bool

**URL:** <https://forums.swift.org/t/adding-toggle-to-bool/7414>\
**Category:** Pitches\
**Created:** [January 12, 2018, 6:15am UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414 "2018-01-12T06:15:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Eidhof](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_eidhof/32/1218_2.png) [@Chris\_Eidhof](https://forums.swift.org/u/Chris_Eidhof)\
**Post date:** [January 12, 2018, 6:15am UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/1 "2018-01-12T06:15:21Z")

</div>

Hey SE!

When we have a bunch of nested structs:

&nbsp;&nbsp;&nbsp;&nbsp;struct Sample {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;struct Bar {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))

It can be repetitive to toggle a deeply nested boolean:

&nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication

I sometimes add a `toggle` extension on `Bool`

&nbsp;&nbsp;&nbsp;&nbsp;extension Bool {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}

This allows you to write the same code without duplication, and makes the  
intent clearer:

&nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show.toggle()

In other languages, I don't think the `toggle` would make as much sense,  
but the mutable self makes this very useful.

After I posted it on Twitter, it turns out I'm not the only one:

> <https://twitter.com/PublicExtension/status/730434956376346624>

I would have gone straight to a proposal, but I think we can do some  
bikeshedding about the name of `toggle`?

> **···**
>
> --  
> Chris Eidhof

---

<div class="post-metadata">

**Author:** ![nnnnnnnn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nnnnnnnn/32/264_2.png) [@nnnnnnnn](https://forums.swift.org/u/nnnnnnnn)\
**Post date:** [January 12, 2018, 8:14am UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/2 "2018-01-12T08:14:13Z")

</div>

> Hey SE!
> 
> When we have a bunch of nested structs:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;struct Sample {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;struct Bar {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> 
> It can be repetitive to toggle a deeply nested boolean:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> 
> I sometimes add a `toggle` extension on `Bool`
> 
> &nbsp;&nbsp;&nbsp;&nbsp;extension Bool {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> This allows you to write the same code without duplication, and makes the intent clearer:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show.toggle()

I like it!

> In other languages, I don't think the `toggle` would make as much sense, but the mutable self makes this very useful.
> 
> After I posted it on Twitter, it turns out I'm not the only one: [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> 
> I would have gone straight to a proposal, but I think we can do some bikeshedding about the name of `toggle`?

Another verb that could work is `invert`.

The `!` operator that does this is the negation operator, but I think `negate` could sound to some like "make this false" rather than toggling.

Nate

> **···**
>
> > On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \<swift-evolution@swift.org\> wrote:

---

<div class="post-metadata">

**Author:** ![masters3d](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@masters3d](https://forums.swift.org/u/masters3d)\
**Post date:** [January 12, 2018, 4:29pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/3 "2018-01-12T16:29:38Z")

</div>

> > Hey SE!
> > 
> > When we have a bunch of nested structs:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;struct Sample {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;struct Bar {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> > 
> > It can be repetitive to toggle a deeply nested boolean:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> > 
> > I sometimes add a `toggle` extension on `Bool`
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension Bool {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > This allows you to write the same code without duplication, and makes the intent clearer:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show.toggle()
> 
> I like it!
> 
> > In other languages, I don't think the `toggle` would make as much sense, but the mutable self makes this very useful.
> > 
> > After I posted it on Twitter, it turns out I'm not the only one: [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > 
> > I would have gone straight to a proposal, but I think we can do some bikeshedding about the name of `toggle`?
> 
> Another verb that could work is `invert`.

That’s a good one. Similar to bit flipping which Apple calls inverting.

“The bitwise NOT operator (~) inverts all bits in a number:”

I was thinking `flip` for this function but then I actually don’t think this extension is a good idea in the standard library :(

Should we also have a mutating extension for the `NOT` operator? Probably no.

If people need it, they can add it and call it toggle, invert, flip, reverse, negate or what ever they want.

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.

> **···**
>
> > On Jan 12, 2018, at 12:14 AM, Nate Cook via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > > On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> > The `!` operator that does this is the negation operator, but I think `negate` could sound to some like "make this false" rather than toggling.
> > 
> > Nate  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [January 12, 2018, 10:13am UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/4 "2018-01-12T10:13:31Z")

</div>

I’m not sure if this would be considered or not, but I would like if the negation operator `!` would fade out.

If this is ever going to a review then I’d suggest that we add a pair of functions, one mutating and the other non-mutating.

```
extension Bool {
  mutating func invert() {
    self = !self
  }

  func inverted() {
    return !self
  }
}

```

I’d rather use `inverted` instead of `!` because of the readability this function provides.

```
if !items.contains(item) { ... }

if items.contains(item).inverted() { ... } 

```

> **···**
>
> ——
> 
> I personally have some other extensions like:
> 
> ```
> extension Bool {
> @discardableResult
> func whenTrue<T>(execute closure: () throws -> T) rethrows -> T? {
> if self { return try closure() }
> return nil
> }
> 
> @discardableResult
> func whenFalse<T>(execute closure: () throws -> T) rethrows -> T? {
> if !self { return try closure() }
> return nil
> }
> }
> 
> ```
> 
> But this is more a personal preference.
> 
> ——
> 
> That said, if the community is fine with the `invert/inverted` pair then I’d say go for it ;)  
> Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution (swift-evolution@swift.org) schrieb:
> 
> On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> Hey SE!
> 
> When we have a bunch of nested structs:
> 
> ```
> struct Sample \{
> var bar: Bar
> \}
> 
> struct Bar \{
> var show: Bool
> \}
> 
> var foo = Sample\(bar: Bar\(show: false\)\)
> 
> ```
> 
> It can be repetitive to toggle a deeply nested boolean:
> 
> ```
> foo\.bar\.show = \!foo\.bar\.show // duplication
> 
> ```
> 
> I sometimes add a `toggle` extension on `Bool`
> 
> ```
> extension Bool \{
> mutating func toggle\(\) \{
> self = \!self
> \}
> \}
> 
> ```
> 
> This allows you to write the same code without duplication, and makes the intent clearer:
> 
> ```
> foo\.bar\.show\.toggle\(\)
> 
> ```
> 
> I like it!
> 
> In other languages, I don't think the `toggle` would make as much sense, but the mutable self makes this very useful.
> 
> After I posted it on Twitter, it turns out I'm not the only one: [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> 
> I would have gone straight to a proposal, but I think we can do some bikeshedding about the name of `toggle`?
> 
> Another verb that could work is `invert`.
> 
> The `!` operator that does this is the negation operator, but I think `negate` could sound to some like "make this false" rather than toggling.
> 
> Nate  
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> swift-evolution mailing list  
> swift-evolution@swift.org  
> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![hartbit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hartbit/32/738_2.png) [@hartbit](https://forums.swift.org/u/hartbit)\
**Post date:** [January 12, 2018, 12:14pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/5 "2018-01-12T12:14:32Z")

</div>

> Hey SE!
> 
> When we have a bunch of nested structs:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;struct Sample {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;struct Bar {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> 
> It can be repetitive to toggle a deeply nested boolean:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> 
> I sometimes add a `toggle` extension on `Bool`
> 
> &nbsp;&nbsp;&nbsp;&nbsp;extension Bool {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> This allows you to write the same code without duplication, and makes the intent clearer:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show.toggle()
> 
> In other languages, I don't think the `toggle` would make as much sense, but the mutable self makes this very useful.
> 
> After I posted it on Twitter, it turns out I'm not the only one: [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> 
> I would have gone straight to a proposal, but I think we can do some bikeshedding about the name of `toggle`?

Out of all the versions I heard, toggle is the one that makes the most sense to me.

> **···**
>
> > On 12 Jan 2018, at 07:15, Chris Eidhof via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> > --  
> > Chris Eidhof  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![David\_Sweeris](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_sweeris/32/31302_2.png) [@David\_Sweeris](https://forums.swift.org/u/David_Sweeris)\
**Post date:** [January 14, 2018, 1:28am UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/6 "2018-01-14T01:28:12Z")

</div>

“Toggle” works for me, if we go this route.

I have a question about the idea, though... Why limit this to Bools? In principle, as long as there aren’t any associated values involved, shouldn’t we be able to iterate through any exhaustive enum type? Bools are an obvious example of a case where “x.nextCase” is useful, but should we consider whether we ought to instead give this functionality to all enums that don’t have associated values?

- Dave Sweeris

> **···**
>
> > On Jan 11, 2018, at 22:15, Chris Eidhof via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Hey SE!
> > 
> > When we have a bunch of nested structs:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;struct Sample {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;struct Bar {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> > 
> > It can be repetitive to toggle a deeply nested boolean:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> > 
> > I sometimes add a `toggle` extension on `Bool`
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension Bool {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > This allows you to write the same code without duplication, and makes the intent clearer:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show.toggle()
> > 
> > In other languages, I don't think the `toggle` would make as much sense, but the mutable self makes this very useful.
> > 
> > After I posted it on Twitter, it turns out I'm not the only one: [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > 
> > I would have gone straight to a proposal, but I think we can do some bikeshedding about the name of `toggle`?

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [January 12, 2018, 5:17pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/7 "2018-01-12T17:17:12Z")

</div>

+1 for toggle, seems the clear winner for me.

> **···**
>
> > On Jan 12, 2018, at 8:29 AM, Cheyo Jimenez via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > 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.
> 
> I would love to have not(), but for a different purpose:
> 
> func not\<T\>(\_ predicate: @escaping (T)-\>Bool) -\> (T)-\>Bool {  
> &nbsp;&nbsp;return { !predicate($0) }  
> }
> 
> let noScrubs = guys.filter(not(isBusta))
> 
> I guess you could overload a version of ! for predicates but that doesn’t seem wise.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 12, 2018, 3:41pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/8 "2018-01-12T15:41:12Z")

</div>

Agree: toggle makes the most sense to me as well.

> **···**
>
> On Fri, Jan 12, 2018 at 06:14 David Hart via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > On 12 Jan 2018, at 07:15, Chris Eidhof via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > Hey SE!
> > 
> > When we have a bunch of nested structs:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;struct Sample {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;struct Bar {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> > 
> > It can be repetitive to toggle a deeply nested boolean:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> > 
> > I sometimes add a `toggle` extension on `Bool`
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension Bool {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > This allows you to write the same code without duplication, and makes the  
> > intent clearer:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show.toggle()
> > 
> > In other languages, I don't think the `toggle` would make as much sense,  
> > but the mutable self makes this very useful.
> > 
> > After I posted it on Twitter, it turns out I'm not the only one:  
> > [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > 
> > I would have gone straight to a proposal, but I think we can do some  
> > bikeshedding about the name of `toggle`?
> > 
> > Out of all the versions I heard, toggle is the one that makes the most  
> > sense to me.
> > 
> > --  
> > Chris Eidhof  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Alejandro\_Martinez](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro_martinez/32/821_2.png) [@Alejandro\_Martinez](https://forums.swift.org/u/Alejandro_Martinez)\
**Post date:** [January 12, 2018, 7:53pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/9 "2018-01-12T19:53:55Z")

</div>

I wouldn't go as far as to ask to fade out ! but in all my code I end  
up doing == false just for readability. That ! knows who to hide  
himself too well :P

> **···**
>
> On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> > I’m not sure if this would be considered or not, but I would like if the  
> > negation operator `!` would fade out.
> > 
> > If this is ever going to a review then I’d suggest that we add a pair of  
> > functions, one mutating and the other non-mutating.
> > 
> > extension Bool {  
> > &nbsp;&nbsp;mutating func invert() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> > &nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;func inverted() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;return !self  
> > &nbsp;&nbsp;}  
> > }
> > 
> > I’d rather use `inverted` instead of `!` because of the readability this  
> > function provides.
> > 
> > if !items.contains(item) { ... }
> > 
> > if items.contains(item).inverted() { ... }
> > 
> > ——
> > 
> > I personally have some other extensions like:
> > 
> > extension Bool {  
> > &nbsp;&nbsp;@discardableResult  
> > &nbsp;&nbsp;func whenTrue\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > &nbsp;&nbsp;&nbsp;&nbsp;if self { return try closure() }  
> > &nbsp;&nbsp;&nbsp;&nbsp;return nil  
> > &nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;@discardableResult  
> > &nbsp;&nbsp;func whenFalse\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > &nbsp;&nbsp;&nbsp;&nbsp;if !self { return try closure() }  
> > &nbsp;&nbsp;&nbsp;&nbsp;return nil  
> > &nbsp;&nbsp;}  
> > }
> > 
> > But this is more a personal preference.
> > 
> > ——
> > 
> > That said, if the community is fine with the `invert/inverted` pair then I’d  
> > say go for it ;)
> > 
> > Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution  
> > (swift-evolution@swift.org) schrieb:
> > 
> > On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \> \<swift-evolution@swift.org\> wrote:
> > 
> > Hey SE!
> > 
> > When we have a bunch of nested structs:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;struct Sample {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;struct Bar {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> > 
> > It can be repetitive to toggle a deeply nested boolean:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> > 
> > I sometimes add a `toggle` extension on `Bool`
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension Bool {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > This allows you to write the same code without duplication, and makes the  
> > intent clearer:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.bar.show.toggle()
> > 
> > I like it!
> > 
> > In other languages, I don't think the `toggle` would make as much sense, but  
> > the mutable self makes this very useful.
> > 
> > After I posted it on Twitter, it turns out I'm not the only one:  
> > [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > 
> > I would have gone straight to a proposal, but I think we can do some  
> > bikeshedding about the name of `toggle`?
> > 
> > Another verb that could work is `invert`.
> > 
> > The `!` operator that does this is the negation operator, but I think  
> > `negate` could sound to some like "make this false" rather than toggling.
> > 
> > Nate  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> 
> --  
> Alejandro Martinez
> 
> > **[Alexito's World](https://alejandromp.com/)**
> >
> > A world of coding 💻, by Alejandro Martinez

---

<div class="post-metadata">

**Author:** ![smartgo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/smartgo/32/344_2.png) [@smartgo](https://forums.swift.org/u/smartgo)\
**Post date:** [January 12, 2018, 10:34pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/10 "2018-01-12T22:34:17Z")

</div>

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:

&nbsp;&nbsp;extension String {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var nonEmpty: Bool { return !self.isEmpty }  
&nbsp;&nbsp;}

&nbsp;&nbsp;if !string.isEmpty { … }

&nbsp;&nbsp;if string.isEmpty.inverted() { … }

&nbsp;&nbsp;if string.nonEmpty { … }

For the case of `contains`, maybe define `lacks`?

&nbsp;&nbsp;if !items.contains(item) { ... }

&nbsp;&nbsp;if items.contains(item).inverted() { ... }

&nbsp;&nbsp;if items.lacks(item) { ... }

Anders Kierulf

> **···**
>
> > On Jan 12, 2018, at 12:54 PM, Alejandro Martinez via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > I wouldn't go as far as to ask to fade out ! but in all my code I end  
> > up doing == false just for readability. That ! knows who to hide  
> > himself too well :P
> > 
> > On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution \> \<swift-evolution@swift.org\> wrote:
> > 
> > > I’m not sure if this would be considered or not, but I would like if the  
> > > negation operator `!` would fade out.
> > > 
> > > If this is ever going to a review then I’d suggest that we add a pair of  
> > > functions, one mutating and the other non-mutating.
> > > 
> > > extension Bool {  
> > > mutating func invert() {  
> > > &nbsp;&nbsp;&nbsp;self = !self  
> > > }
> > > 
> > > func inverted() {  
> > > &nbsp;&nbsp;&nbsp;return !self  
> > > }  
> > > }
> > > 
> > > I’d rather use `inverted` instead of `!` because of the readability this  
> > > function provides.
> > > 
> > > if !items.contains(item) { ... }
> > > 
> > > if items.contains(item).inverted() { ... }
> > > 
> > > ——
> > > 
> > > I personally have some other extensions like:
> > > 
> > > extension Bool {  
> > > @discardableResult  
> > > func whenTrue\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > &nbsp;&nbsp;&nbsp;if self { return try closure() }  
> > > &nbsp;&nbsp;&nbsp;return nil  
> > > }
> > > 
> > > @discardableResult  
> > > func whenFalse\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > &nbsp;&nbsp;&nbsp;if !self { return try closure() }  
> > > &nbsp;&nbsp;&nbsp;return nil  
> > > }  
> > > }
> > > 
> > > But this is more a personal preference.
> > > 
> > > ——
> > > 
> > > That said, if the community is fine with the `invert/inverted` pair then I’d  
> > > say go for it ;)
> > > 
> > > Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution  
> > > (swift-evolution@swift.org) schrieb:
> > > 
> > > On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \>\> \<swift-evolution@swift.org\> wrote:
> > > 
> > > Hey SE!
> > > 
> > > When we have a bunch of nested structs:
> > > 
> > > &nbsp;&nbsp;&nbsp;struct Sample {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > &nbsp;&nbsp;&nbsp;struct Bar {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > &nbsp;&nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> > > 
> > > It can be repetitive to toggle a deeply nested boolean:
> > > 
> > > &nbsp;&nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> > > 
> > > I sometimes add a `toggle` extension on `Bool`
> > > 
> > > &nbsp;&nbsp;&nbsp;extension Bool {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > This allows you to write the same code without duplication, and makes the  
> > > intent clearer:
> > > 
> > > &nbsp;&nbsp;&nbsp;foo.bar.show.toggle()
> > > 
> > > I like it!
> > > 
> > > In other languages, I don't think the `toggle` would make as much sense, but  
> > > the mutable self makes this very useful.
> > > 
> > > After I posted it on Twitter, it turns out I'm not the only one:  
> > > [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > > 
> > > I would have gone straight to a proposal, but I think we can do some  
> > > bikeshedding about the name of `toggle`?
> > > 
> > > Another verb that could work is `invert`.
> > > 
> > > The `!` operator that does this is the negation operator, but I think  
> > > `negate` could sound to some like "make this false" rather than toggling.
> > > 
> > > Nate  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > --  
> > Alejandro Martinez  
> > [http://alejandromp.com](http://alejandromp.com)  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [January 12, 2018, 7:56pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/11 "2018-01-12T19:56:43Z")

</div>

All I meant is that it’s usage should fade, which is my personal opinion. That said, I didn’t meant to say it should be removed or something. ;)

Am 12. Januar 2018 um 20:54:16, Alejandro Martinez (alexito4@gmail.com) schrieb:

I wouldn't go as far as to ask to fade out ! but in all my code I end  
up doing == false just for readability. That ! knows who to hide  
himself too well :P

> **···**
>
> On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> > I’m not sure if this would be considered or not, but I would like if the  
> > negation operator `!` would fade out.
> > 
> > If this is ever going to a review then I’d suggest that we add a pair of  
> > functions, one mutating and the other non-mutating.
> > 
> > extension Bool {  
> > mutating func invert() {  
> > self = !self  
> > }
> > 
> > func inverted() {  
> > return !self  
> > }  
> > }
> > 
> > I’d rather use `inverted` instead of `!` because of the readability this  
> > function provides.
> > 
> > if !items.contains(item) { ... }
> > 
> > if items.contains(item).inverted() { ... }
> > 
> > ——
> > 
> > I personally have some other extensions like:
> > 
> > extension Bool {  
> > @discardableResult  
> > func whenTrue\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > if self { return try closure() }  
> > return nil  
> > }
> > 
> > @discardableResult  
> > func whenFalse\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > if !self { return try closure() }  
> > return nil  
> > }  
> > }
> > 
> > But this is more a personal preference.
> > 
> > ——
> > 
> > That said, if the community is fine with the `invert/inverted` pair then I’d  
> > say go for it ;)
> > 
> > Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution  
> > (swift-evolution@swift.org) schrieb:
> > 
> > On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \> \<swift-evolution@swift.org\> wrote:
> > 
> > Hey SE!
> > 
> > When we have a bunch of nested structs:
> > 
> > struct Sample {  
> > var bar: Bar  
> > }
> > 
> > struct Bar {  
> > var show: Bool  
> > }
> > 
> > var foo = Sample(bar: Bar(show: false))
> > 
> > It can be repetitive to toggle a deeply nested boolean:
> > 
> > foo.bar.show = !foo.bar.show // duplication
> > 
> > I sometimes add a `toggle` extension on `Bool`
> > 
> > extension Bool {  
> > mutating func toggle() {  
> > self = !self  
> > }  
> > }
> > 
> > This allows you to write the same code without duplication, and makes the  
> > intent clearer:
> > 
> > foo.bar.show.toggle()
> > 
> > I like it!
> > 
> > In other languages, I don't think the `toggle` would make as much sense, but  
> > the mutable self makes this very useful.
> > 
> > After I posted it on Twitter, it turns out I'm not the only one:  
> > [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > 
> > I would have gone straight to a proposal, but I think we can do some  
> > bikeshedding about the name of `toggle`?
> > 
> > Another verb that could work is `invert`.
> > 
> > The `!` operator that does this is the negation operator, but I think  
> > `negate` could sound to some like "make this false" rather than toggling.
> > 
> > Nate  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> 
> --  
> Alejandro Martinez
> 
> > **[Alexito's World](https://alejandromp.com/)**
> >
> > A world of coding 💻, by Alejandro Martinez

---

<div class="post-metadata">

**Author:** ![Chris\_Eidhof](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_eidhof/32/1218_2.png) [@Chris\_Eidhof](https://forums.swift.org/u/Chris_Eidhof)\
**Post date:** [January 13, 2018, 6:14pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/12 "2018-01-13T18:14:18Z")

</div>

Thanks for all the feedback everyone, I just submitted a short proposal as  
a PR: [Bool toggle proposal by chriseidhof · Pull Request #782 · apple/swift-evolution · GitHub](https://github.com/apple/swift-evolution/pull/782)

> **···**
>
> On Fri, Jan 12, 2018 at 11:34 PM, Anders Kierulf via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > 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:
> > 
> > &nbsp;&nbsp;extension String {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var nonEmpty: Bool { return !self.isEmpty }  
> > &nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;if !string.isEmpty { … }
> > 
> > &nbsp;&nbsp;if string.isEmpty.inverted() { … }
> > 
> > &nbsp;&nbsp;if string.nonEmpty { … }
> > 
> > For the case of `contains`, maybe define `lacks`?
> > 
> > &nbsp;&nbsp;if !items.contains(item) { ... }
> > 
> > &nbsp;&nbsp;if items.contains(item).inverted() { ... }
> > 
> > &nbsp;&nbsp;if items.lacks(item) { ... }
> > 
> > Anders Kierulf
> > 
> > \> On Jan 12, 2018, at 12:54 PM, Alejandro Martinez via swift-evolution \< \> swift-evolution@swift.org\> wrote:  
> > \>  
> > \> I wouldn't go as far as to ask to fade out ! but in all my code I end  
> > \> up doing == false just for readability. That ! knows who to hide  
> > \> himself too well :P  
> > \>  
> > \> On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution \> \> \<swift-evolution@swift.org\> wrote:  
> > \>\> I’m not sure if this would be considered or not, but I would like if the  
> > \>\> negation operator `!` would fade out.  
> > \>\>  
> > \>\> If this is ever going to a review then I’d suggest that we add a pair of  
> > \>\> functions, one mutating and the other non-mutating.  
> > \>\>  
> > \>\> extension Bool {  
> > \>\> mutating func invert() {  
> > \>\> self = !self  
> > \>\> }  
> > \>\>  
> > \>\> func inverted() {  
> > \>\> return !self  
> > \>\> }  
> > \>\> }  
> > \>\>  
> > \>\> I’d rather use `inverted` instead of `!` because of the readability this  
> > \>\> function provides.  
> > \>\>  
> > \>\> if !items.contains(item) { ... }  
> > \>\>  
> > \>\> if items.contains(item).inverted() { ... }  
> > \>\>  
> > \>\> ——  
> > \>\>  
> > \>\> I personally have some other extensions like:  
> > \>\>  
> > \>\> extension Bool {  
> > \>\> @discardableResult  
> > \>\> func whenTrue\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > \>\> if self { return try closure() }  
> > \>\> return nil  
> > \>\> }  
> > \>\>  
> > \>\> @discardableResult  
> > \>\> func whenFalse\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > \>\> if !self { return try closure() }  
> > \>\> return nil  
> > \>\> }  
> > \>\> }  
> > \>\>  
> > \>\> But this is more a personal preference.  
> > \>\>  
> > \>\> ——  
> > \>\>  
> > \>\> That said, if the community is fine with the `invert/inverted` pair  
> > then I’d  
> > \>\> say go for it ;)  
> > \>\>  
> > \>\> Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution  
> > \>\> (swift-evolution@swift.org) schrieb:  
> > \>\>  
> > \>\>  
> > \>\> On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \> \>\> \<swift-evolution@swift.org\> wrote:  
> > \>\>  
> > \>\> Hey SE!  
> > \>\>  
> > \>\> When we have a bunch of nested structs:  
> > \>\>  
> > \>\> struct Sample {  
> > \>\> var bar: Bar  
> > \>\> }  
> > \>\>  
> > \>\> struct Bar {  
> > \>\> var show: Bool  
> > \>\> }  
> > \>\>  
> > \>\> var foo = Sample(bar: Bar(show: false))  
> > \>\>  
> > \>\> It can be repetitive to toggle a deeply nested boolean:  
> > \>\>  
> > \>\> foo.bar.show = !foo.bar.show // duplication  
> > \>\>  
> > \>\> I sometimes add a `toggle` extension on `Bool`  
> > \>\>  
> > \>\> extension Bool {  
> > \>\> mutating func toggle() {  
> > \>\> self = !self  
> > \>\> }  
> > \>\> }  
> > \>\>  
> > \>\> This allows you to write the same code without duplication, and makes  
> > the  
> > \>\> intent clearer:  
> > \>\>  
> > \>\> foo.bar.show.toggle()  
> > \>\>  
> > \>\>  
> > \>\> I like it!  
> > \>\>  
> > \>\> In other languages, I don't think the `toggle` would make as much  
> > sense, but  
> > \>\> the mutable self makes this very useful.  
> > \>\>  
> > \>\> After I posted it on Twitter, it turns out I'm not the only one:  
> > \>\> [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)  
> > \>\>  
> > \>\> I would have gone straight to a proposal, but I think we can do some  
> > \>\> bikeshedding about the name of `toggle`?  
> > \>\>  
> > \>\>  
> > \>\> Another verb that could work is `invert`.  
> > \>\>  
> > \>\> The `!` operator that does this is the negation operator, but I think  
> > \>\> `negate` could sound to some like "make this false" rather than  
> > toggling.  
> > \>\>  
> > \>\> Nate  
> > \>\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \>\> swift-evolution mailing list  
> > \>\> swift-evolution@swift.org  
> > \>\> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \>\>  
> > \>\>  
> > \>\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \>\> swift-evolution mailing list  
> > \>\> swift-evolution@swift.org  
> > \>\> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \>\>  
> > \>  
> > \>  
> > \>  
> > \> --  
> > \> Alejandro Martinez  
> > \> [http://alejandromp.com](http://alejandromp.com)  
> > \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \> swift-evolution mailing list  
> > \> swift-evolution@swift.org  
> > \> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> 
> --  
> Chris Eidhof

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [January 13, 2018, 7:06pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/13 "2018-01-13T19:06:18Z")

</div>

> I wouldn't go as far as to ask to fade out ! but in all my code I end  
> up doing == false just for readability. That ! knows who to hide  
> himself too well :P

Yeah so do I. ! is a very narrow character and totally changes the meaning of the logic.

That said, I can’t come up with a clearer name than “== false”. inverted() isn’t helpful. toggle() on a mutable Bool is good, though.

- Karl

> **···**
>
> > On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> > On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution \> \<swift-evolution@swift.org\> wrote:
> > 
> > > I’m not sure if this would be considered or not, but I would like if the  
> > > negation operator `!` would fade out.
> > > 
> > > If this is ever going to a review then I’d suggest that we add a pair of  
> > > functions, one mutating and the other non-mutating.
> > > 
> > > extension Bool {  
> > > mutating func invert() {  
> > > &nbsp;&nbsp;&nbsp;self = !self  
> > > }
> > > 
> > > func inverted() {  
> > > &nbsp;&nbsp;&nbsp;return !self  
> > > }  
> > > }
> > > 
> > > I’d rather use `inverted` instead of `!` because of the readability this  
> > > function provides.
> > > 
> > > if !items.contains(item) { ... }
> > > 
> > > if items.contains(item).inverted() { ... }
> > > 
> > > ——
> > > 
> > > I personally have some other extensions like:
> > > 
> > > extension Bool {  
> > > @discardableResult  
> > > func whenTrue\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > &nbsp;&nbsp;&nbsp;if self { return try closure() }  
> > > &nbsp;&nbsp;&nbsp;return nil  
> > > }
> > > 
> > > @discardableResult  
> > > func whenFalse\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > &nbsp;&nbsp;&nbsp;if !self { return try closure() }  
> > > &nbsp;&nbsp;&nbsp;return nil  
> > > }  
> > > }
> > > 
> > > But this is more a personal preference.
> > > 
> > > ——
> > > 
> > > That said, if the community is fine with the `invert/inverted` pair then I’d  
> > > say go for it ;)
> > > 
> > > Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution  
> > > (swift-evolution@swift.org) schrieb:
> > > 
> > > On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \>\> \<swift-evolution@swift.org\> wrote:
> > > 
> > > Hey SE!
> > > 
> > > When we have a bunch of nested structs:
> > > 
> > > &nbsp;&nbsp;&nbsp;struct Sample {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > &nbsp;&nbsp;&nbsp;struct Bar {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > &nbsp;&nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> > > 
> > > It can be repetitive to toggle a deeply nested boolean:
> > > 
> > > &nbsp;&nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> > > 
> > > I sometimes add a `toggle` extension on `Bool`
> > > 
> > > &nbsp;&nbsp;&nbsp;extension Bool {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > This allows you to write the same code without duplication, and makes the  
> > > intent clearer:
> > > 
> > > &nbsp;&nbsp;&nbsp;foo.bar.show.toggle()
> > > 
> > > I like it!
> > > 
> > > In other languages, I don't think the `toggle` would make as much sense, but  
> > > the mutable self makes this very useful.
> > > 
> > > After I posted it on Twitter, it turns out I'm not the only one:  
> > > [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > > 
> > > I would have gone straight to a proposal, but I think we can do some  
> > > bikeshedding about the name of `toggle`?
> > > 
> > > Another verb that could work is `invert`.
> > > 
> > > The `!` operator that does this is the negation operator, but I think  
> > > `negate` could sound to some like "make this false" rather than toggling.
> > > 
> > > Nate  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > --  
> > Alejandro Martinez  
> > [http://alejandromp.com](http://alejandromp.com)  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![eneko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eneko/32/12117_2.png) [@eneko](https://forums.swift.org/u/eneko)\
**Post date:** [January 14, 2018, 7:28am UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/14 "2018-01-14T07:28:07Z")

</div>

I am yet another one that tries to never use !

It feels like bad heritage from C, and it probably should be removed from Swift in the same way for(;;) and ++/-- where removed.

! does not provide any unique functionality, as it is redundant to “== false”. Other than syntax sugar, it does not add any value to the language.

I feel a mutating toggle(), invert() or flip() method on Bool provides much more value. The non-mutating counterpart however should probably not be included in the standard library, as it would also be redundant to “== false”.

Eneko Alonso

> **···**
>
> > On Jan 12, 2018, at 14:34, Anders Kierulf via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > 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:
> > 
> > extension String {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var nonEmpty: Bool { return !self.isEmpty }  
> > }
> > 
> > if !string.isEmpty { … }
> > 
> > if string.isEmpty.inverted() { … }
> > 
> > if string.nonEmpty { … }
> > 
> > For the case of `contains`, maybe define `lacks`?
> > 
> > if !items.contains(item) { ... }
> > 
> > if items.contains(item).inverted() { ... }
> > 
> > if items.lacks(item) { ... }
> > 
> > Anders Kierulf
> > 
> > > On Jan 12, 2018, at 12:54 PM, Alejandro Martinez via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > I wouldn't go as far as to ask to fade out ! but in all my code I end  
> > > up doing == false just for readability. That ! knows who to hide  
> > > himself too well :P
> > > 
> > > On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution \>\> \<swift-evolution@swift.org\> wrote:
> > > 
> > > > I’m not sure if this would be considered or not, but I would like if the  
> > > > negation operator `!` would fade out.
> > > > 
> > > > If this is ever going to a review then I’d suggest that we add a pair of  
> > > > functions, one mutating and the other non-mutating.
> > > > 
> > > > extension Bool {  
> > > > mutating func invert() {  
> > > > &nbsp;&nbsp;self = !self  
> > > > }
> > > > 
> > > > func inverted() {  
> > > > &nbsp;&nbsp;return !self  
> > > > }  
> > > > }
> > > > 
> > > > I’d rather use `inverted` instead of `!` because of the readability this  
> > > > function provides.
> > > > 
> > > > if !items.contains(item) { ... }
> > > > 
> > > > if items.contains(item).inverted() { ... }
> > > > 
> > > > ——
> > > > 
> > > > I personally have some other extensions like:
> > > > 
> > > > extension Bool {  
> > > > @discardableResult  
> > > > func whenTrue\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > > &nbsp;&nbsp;if self { return try closure() }  
> > > > &nbsp;&nbsp;return nil  
> > > > }
> > > > 
> > > > @discardableResult  
> > > > func whenFalse\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > > &nbsp;&nbsp;if !self { return try closure() }  
> > > > &nbsp;&nbsp;return nil  
> > > > }  
> > > > }
> > > > 
> > > > But this is more a personal preference.
> > > > 
> > > > ——
> > > > 
> > > > That said, if the community is fine with the `invert/inverted` pair then I’d  
> > > > say go for it ;)
> > > > 
> > > > Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution  
> > > > (swift-evolution@swift.org) schrieb:
> > > > 
> > > > On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \>\>\> \<swift-evolution@swift.org\> wrote:
> > > > 
> > > > Hey SE!
> > > > 
> > > > When we have a bunch of nested structs:
> > > > 
> > > > &nbsp;&nbsp;struct Sample {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> > > > &nbsp;&nbsp;}
> > > > 
> > > > &nbsp;&nbsp;struct Bar {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> > > > &nbsp;&nbsp;}
> > > > 
> > > > &nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> > > > 
> > > > It can be repetitive to toggle a deeply nested boolean:
> > > > 
> > > > &nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> > > > 
> > > > I sometimes add a `toggle` extension on `Bool`
> > > > 
> > > > &nbsp;&nbsp;extension Bool {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > &nbsp;&nbsp;}
> > > > 
> > > > This allows you to write the same code without duplication, and makes the  
> > > > intent clearer:
> > > > 
> > > > &nbsp;&nbsp;foo.bar.show.toggle()
> > > > 
> > > > I like it!
> > > > 
> > > > In other languages, I don't think the `toggle` would make as much sense, but  
> > > > the mutable self makes this very useful.
> > > > 
> > > > After I posted it on Twitter, it turns out I'm not the only one:  
> > > > [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > > > 
> > > > I would have gone straight to a proposal, but I think we can do some  
> > > > bikeshedding about the name of `toggle`?
> > > > 
> > > > Another verb that could work is `invert`.
> > > > 
> > > > The `!` operator that does this is the negation operator, but I think  
> > > > `negate` could sound to some like "make this false" rather than toggling.
> > > > 
> > > > Nate  
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > --  
> > > Alejandro Martinez  
> > > [http://alejandromp.com](http://alejandromp.com)  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![ckeithray](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ckeithray/32/2317_2.png) [@ckeithray](https://forums.swift.org/u/ckeithray)\
**Post date:** [January 13, 2018, 7:09pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/15 "2018-01-13T19:09:32Z")

</div>

If you would like something better than "== false" I suggest adding computed properties 'isTrue' and 'isFalse' to Boolean.

> **···**
>
> > On Jan 13, 2018, at 11:06 AM, Karl Wagner via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > > On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > 
> > > I wouldn't go as far as to ask to fade out ! but in all my code I end  
> > > up doing == false just for readability. That ! knows who to hide  
> > > himself too well :P
> > 
> > Yeah so do I. ! is a very narrow character and totally changes the meaning of the logic.
> > 
> > That said, I can’t come up with a clearer name than “== false”. inverted() isn’t helpful. toggle() on a mutable Bool is good, though.
> > 
> > - Karl
> > 
> > > On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution \>\> \<swift-evolution@swift.org\> wrote:
> > > 
> > > > I’m not sure if this would be considered or not, but I would like if the  
> > > > negation operator `!` would fade out.
> > > > 
> > > > If this is ever going to a review then I’d suggest that we add a pair of  
> > > > functions, one mutating and the other non-mutating.
> > > > 
> > > > extension Bool {  
> > > > mutating func invert() {  
> > > > &nbsp;&nbsp;self = !self  
> > > > }
> > > > 
> > > > func inverted() {  
> > > > &nbsp;&nbsp;return !self  
> > > > }  
> > > > }
> > > > 
> > > > I’d rather use `inverted` instead of `!` because of the readability this  
> > > > function provides.
> > > > 
> > > > if !items.contains(item) { ... }
> > > > 
> > > > if items.contains(item).inverted() { ... }
> > > > 
> > > > ——
> > > > 
> > > > I personally have some other extensions like:
> > > > 
> > > > extension Bool {  
> > > > @discardableResult  
> > > > func whenTrue\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > > &nbsp;&nbsp;if self { return try closure() }  
> > > > &nbsp;&nbsp;return nil  
> > > > }
> > > > 
> > > > @discardableResult  
> > > > func whenFalse\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > > &nbsp;&nbsp;if !self { return try closure() }  
> > > > &nbsp;&nbsp;return nil  
> > > > }  
> > > > }
> > > > 
> > > > But this is more a personal preference.
> > > > 
> > > > ——
> > > > 
> > > > That said, if the community is fine with the `invert/inverted` pair then I’d  
> > > > say go for it ;)
> > > > 
> > > > Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution  
> > > > (swift-evolution@swift.org) schrieb:
> > > > 
> > > > On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \>\>\> \<swift-evolution@swift.org\> wrote:
> > > > 
> > > > Hey SE!
> > > > 
> > > > When we have a bunch of nested structs:
> > > > 
> > > > &nbsp;&nbsp;struct Sample {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> > > > &nbsp;&nbsp;}
> > > > 
> > > > &nbsp;&nbsp;struct Bar {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> > > > &nbsp;&nbsp;}
> > > > 
> > > > &nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> > > > 
> > > > It can be repetitive to toggle a deeply nested boolean:
> > > > 
> > > > &nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> > > > 
> > > > I sometimes add a `toggle` extension on `Bool`
> > > > 
> > > > &nbsp;&nbsp;extension Bool {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > &nbsp;&nbsp;}
> > > > 
> > > > This allows you to write the same code without duplication, and makes the  
> > > > intent clearer:
> > > > 
> > > > &nbsp;&nbsp;foo.bar.show.toggle()
> > > > 
> > > > I like it!
> > > > 
> > > > In other languages, I don't think the `toggle` would make as much sense, but  
> > > > the mutable self makes this very useful.
> > > > 
> > > > After I posted it on Twitter, it turns out I'm not the only one:  
> > > > [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > > > 
> > > > I would have gone straight to a proposal, but I think we can do some  
> > > > bikeshedding about the name of `toggle`?
> > > > 
> > > > Another verb that could work is `invert`.
> > > > 
> > > > The `!` operator that does this is the negation operator, but I think  
> > > > `negate` could sound to some like "make this false" rather than toggling.
> > > > 
> > > > Nate  
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > --  
> > > Alejandro Martinez  
> > > [http://alejandromp.com](http://alejandromp.com)  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![ckeithray](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ckeithray/32/2317_2.png) [@ckeithray](https://forums.swift.org/u/ckeithray)\
**Post date:** [January 13, 2018, 7:10pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/16 "2018-01-13T19:10:54Z")

</div>

(I know isTrue is redundant. I only considered it for symmetry.)

> **···**
>
> > On Jan 13, 2018, at 11:09 AM, C. Keith Ray \<keithray@mac.com\> wrote:
> > 
> > If you would like something better than "== false" I suggest adding computed properties 'isTrue' and 'isFalse' to Boolean.
> > 
> > > On Jan 13, 2018, at 11:06 AM, Karl Wagner via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > 
> > > > On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > > 
> > > > I wouldn't go as far as to ask to fade out ! but in all my code I end  
> > > > up doing == false just for readability. That ! knows who to hide  
> > > > himself too well :P
> > > 
> > > Yeah so do I. ! is a very narrow character and totally changes the meaning of the logic.
> > > 
> > > That said, I can’t come up with a clearer name than “== false”. inverted() isn’t helpful. toggle() on a mutable Bool is good, though.
> > > 
> > > - Karl
> > > 
> > > > On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution \>\>\> \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > > 
> > > > > I’m not sure if this would be considered or not, but I would like if the  
> > > > > negation operator `!` would fade out.
> > > > > 
> > > > > If this is ever going to a review then I’d suggest that we add a pair of  
> > > > > functions, one mutating and the other non-mutating.
> > > > > 
> > > > > extension Bool {  
> > > > > mutating func invert() {  
> > > > > &nbsp;&nbsp;self = !self  
> > > > > }
> > > > > 
> > > > > func inverted() {  
> > > > > &nbsp;&nbsp;return !self  
> > > > > }  
> > > > > }
> > > > > 
> > > > > I’d rather use `inverted` instead of `!` because of the readability this  
> > > > > function provides.
> > > > > 
> > > > > if !items.contains(item) { ... }
> > > > > 
> > > > > if items.contains(item).inverted() { ... }
> > > > > 
> > > > > ——
> > > > > 
> > > > > I personally have some other extensions like:
> > > > > 
> > > > > extension Bool {  
> > > > > @discardableResult  
> > > > > func whenTrue\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > > > &nbsp;&nbsp;if self { return try closure() }  
> > > > > &nbsp;&nbsp;return nil  
> > > > > }
> > > > > 
> > > > > @discardableResult  
> > > > > func whenFalse\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > > > &nbsp;&nbsp;if !self { return try closure() }  
> > > > > &nbsp;&nbsp;return nil  
> > > > > }  
> > > > > }
> > > > > 
> > > > > But this is more a personal preference.
> > > > > 
> > > > > ——
> > > > > 
> > > > > That said, if the community is fine with the `invert/inverted` pair then I’d  
> > > > > say go for it ;)
> > > > > 
> > > > > Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution  
> > > > > (swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>) schrieb:
> > > > > 
> > > > > On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \>\>\>\> \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > > > 
> > > > > Hey SE!
> > > > > 
> > > > > When we have a bunch of nested structs:
> > > > > 
> > > > > &nbsp;&nbsp;struct Sample {  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bar: Bar  
> > > > > &nbsp;&nbsp;}
> > > > > 
> > > > > &nbsp;&nbsp;struct Bar {  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var show: Bool  
> > > > > &nbsp;&nbsp;}
> > > > > 
> > > > > &nbsp;&nbsp;var foo = Sample(bar: Bar(show: false))
> > > > > 
> > > > > It can be repetitive to toggle a deeply nested boolean:
> > > > > 
> > > > > &nbsp;&nbsp;foo.bar.show = !foo.bar.show // duplication
> > > > > 
> > > > > I sometimes add a `toggle` extension on `Bool`
> > > > > 
> > > > > &nbsp;&nbsp;extension Bool {  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating func toggle() {  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = !self  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > > &nbsp;&nbsp;}
> > > > > 
> > > > > This allows you to write the same code without duplication, and makes the  
> > > > > intent clearer:
> > > > > 
> > > > > &nbsp;&nbsp;foo.bar.show.toggle()
> > > > > 
> > > > > I like it!
> > > > > 
> > > > > In other languages, I don't think the `toggle` would make as much sense, but  
> > > > > the mutable self makes this very useful.
> > > > > 
> > > > > After I posted it on Twitter, it turns out I'm not the only one:  
> > > > > [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > > > > 
> > > > > I would have gone straight to a proposal, but I think we can do some  
> > > > > bikeshedding about the name of `toggle`?
> > > > > 
> > > > > Another verb that could work is `invert`.
> > > > > 
> > > > > The `!` operator that does this is the negation operator, but I think  
> > > > > `negate` could sound to some like "make this false" rather than toggling.
> > > > > 
> > > > > Nate  
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-evolution mailing list  
> > > > > swift-evolution@swift.org  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > > 
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-evolution mailing list  
> > > > > swift-evolution@swift.org  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > 
> > > > --  
> > > > Alejandro Martinez  
> > > > [http://alejandromp.com](http://alejandromp.com) \<[http://alejandromp.com/&gt](http://alejandromp.com/&gt);  
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [January 13, 2018, 9:54pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/17 "2018-01-13T21:54:29Z")

</div>

I prefer `toggle()`/`toggled()` to `invert()`/`inverted()`. I could go on about why but I'm not 100% that this extension adds sufficient utility that it passes the inclusion bar. I would vote against adding them under either name.

I feel more strongly about `.isTrue`/`.isFalse`. I don't think we need them.

-- E

> **···**
>
> On Jan 13, 2018, at 12:06 PM, Karl Wagner via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> > > On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > 
> > > I wouldn't go as far as to ask to fade out ! but in all my code I end  
> > > up doing == false just for readability. That ! knows who to hide  
> > > himself too well :P
> > 
> > Yeah so do I. ! is a very narrow character and totally changes the meaning of the logic.
> > 
> > That said, I can’t come up with a clearer name than “== false”. inverted() isn’t helpful. toggle() on a mutable Bool is good, though.
> > 
> > - Karl
> 
> > > Nate:  
> > > I would have gone straight to a proposal, but I think we can do some  
> > > bikeshedding about the name of `toggle`?
> > > 
> > > Another verb that could work is `invert`.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [January 13, 2018, 7:08pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/18 "2018-01-13T19:08:47Z")

</div>

Well I only used the previously mentioned names, in my codebase I use `isFalse` for that situation which is better then `== false` in my opinion.

Am 13. Januar 2018 um 20:06:21, Karl Wagner (razielim@gmail.com) schrieb:

> I wouldn't go as far as to ask to fade out ! but in all my code I end  
> up doing == false just for readability. That ! knows who to hide  
> himself too well :P

Yeah so do I. ! is a very narrow character and totally changes the meaning of the logic.

That said, I can’t come up with a clearer name than “== false”. inverted() isn’t helpful. toggle() on a mutable Bool is good, though.

- Karl

> **···**
>
> > On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> > On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution \> \<swift-evolution@swift.org\> wrote:
> > 
> > > I’m not sure if this would be considered or not, but I would like if the  
> > > negation operator `!` would fade out.
> > > 
> > > If this is ever going to a review then I’d suggest that we add a pair of  
> > > functions, one mutating and the other non-mutating.
> > > 
> > > extension Bool {  
> > > mutating func invert() {  
> > > self = !self  
> > > }
> > > 
> > > func inverted() {  
> > > return !self  
> > > }  
> > > }
> > > 
> > > I’d rather use `inverted` instead of `!` because of the readability this  
> > > function provides.
> > > 
> > > if !items.contains(item) { ... }
> > > 
> > > if items.contains(item).inverted() { ... }
> > > 
> > > ——
> > > 
> > > I personally have some other extensions like:
> > > 
> > > extension Bool {  
> > > @discardableResult  
> > > func whenTrue\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > if self { return try closure() }  
> > > return nil  
> > > }
> > > 
> > > @discardableResult  
> > > func whenFalse\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> > > if !self { return try closure() }  
> > > return nil  
> > > }  
> > > }
> > > 
> > > But this is more a personal preference.
> > > 
> > > ——
> > > 
> > > That said, if the community is fine with the `invert/inverted` pair then I’d  
> > > say go for it ;)
> > > 
> > > Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution  
> > > (swift-evolution@swift.org) schrieb:
> > > 
> > > On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \>\> \<swift-evolution@swift.org\> wrote:
> > > 
> > > Hey SE!
> > > 
> > > When we have a bunch of nested structs:
> > > 
> > > struct Sample {  
> > > var bar: Bar  
> > > }
> > > 
> > > struct Bar {  
> > > var show: Bool  
> > > }
> > > 
> > > var foo = Sample(bar: Bar(show: false))
> > > 
> > > It can be repetitive to toggle a deeply nested boolean:
> > > 
> > > foo.bar.show = !foo.bar.show // duplication
> > > 
> > > I sometimes add a `toggle` extension on `Bool`
> > > 
> > > extension Bool {  
> > > mutating func toggle() {  
> > > self = !self  
> > > }  
> > > }
> > > 
> > > This allows you to write the same code without duplication, and makes the  
> > > intent clearer:
> > > 
> > > foo.bar.show.toggle()
> > > 
> > > I like it!
> > > 
> > > In other languages, I don't think the `toggle` would make as much sense, but  
> > > the mutable self makes this very useful.
> > > 
> > > After I posted it on Twitter, it turns out I'm not the only one:  
> > > [https://twitter.com/PublicExtension/status/730434956376346624](https://twitter.com/PublicExtension/status/730434956376346624)
> > > 
> > > I would have gone straight to a proposal, but I think we can do some  
> > > bikeshedding about the name of `toggle`?
> > > 
> > > Another verb that could work is `invert`.
> > > 
> > > The `!` operator that does this is the negation operator, but I think  
> > > `negate` could sound to some like "make this false" rather than toggling.
> > > 
> > > Nate  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > --  
> > Alejandro Martinez  
> > [http://alejandromp.com](http://alejandromp.com)  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [January 13, 2018, 7:11pm UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/19 "2018-01-13T19:11:11Z")

</div>

The trues is that `isTrue` doesn’t really make sense because boolean instances are named in a way where `isTrue` is rendered redundant.

> **···**
>
> Am 13. Januar 2018 um 20:09:41, C. Keith Ray via swift-evolution (swift-evolution@swift.org) schrieb:
> 
> If you would like something better than "== false" I suggest adding computed properties 'isTrue' and 'isFalse' to Boolean.
> 
> On Jan 13, 2018, at 11:06 AM, Karl Wagner via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> I wouldn't go as far as to ask to fade out ! but in all my code I end  
> up doing == false just for readability. That ! knows who to hide  
> himself too well :P
> 
> Yeah so do I. ! is a very narrow character and totally changes the meaning of the logic.
> 
> That said, I can’t come up with a clearer name than “== false”. inverted() isn’t helpful. toggle() on a mutable Bool is good, though.
> 
> - Karl
> 
> On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution \<swift-evolution@swift.org\> wrote:  
> I’m not sure if this would be considered or not, but I would like if the  
> negation operator `!` would fade out.
> 
> If this is ever going to a review then I’d suggest that we add a pair of  
> functions, one mutating and the other non-mutating.
> 
> extension Bool {  
> mutating func invert() {  
> self = !self  
> }
> 
> func inverted() {  
> return !self  
> }  
> }
> 
> I’d rather use `inverted` instead of `!` because of the readability this  
> function provides.
> 
> if !items.contains(item) { ... }
> 
> if items.contains(item).inverted() { ... }
> 
> ——
> 
> I personally have some other extensions like:
> 
> extension Bool {  
> @discardableResult  
> func whenTrue\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> if self { return try closure() }  
> return nil  
> }
> 
> @discardableResult  
> func whenFalse\<T\>(execute closure: () throws -\> T) rethrows -\> T? {  
> if !self { return try closure() }  
> return nil  
> }  
> }
> 
> But this is more a personal preference.
> 
> ——
> 
> That said, if the community is fine with the `invert/inverted` pair then I’d  
> say go for it ;)
> 
> Am 12. Januar 2018 um 09:14:22, Nate Cook via swift-evolution  
> (swift-evolution@swift.org) schrieb:
> 
> On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> Hey SE!
> 
> When we have a bunch of nested structs:
> 
> struct Sample {  
> var bar: Bar  
> }
> 
> struct Bar {  
> var show: Bool  
> }
> 
> var foo = Sample(bar: Bar(show: false))
> 
> It can be repetitive to toggle a deeply nested boolean:
> 
> foo.bar.show = !foo.bar.show // duplication
> 
> I sometimes add a `toggle` extension on `Bool`
> 
> extension Bool {  
> mutating func toggle() {  
> self = !self  
> }  
> }
> 
> This allows you to write the same code without duplication, and makes the  
> intent clearer:
> 
> foo.bar.show.toggle()
> 
> I like it!
> 
> In other languages, I don't think the `toggle` would make as much sense, but  
> the mutable self makes this very useful.
> 
> After I posted it on Twitter, it turns out I'm not the only one:
> 
> > <https://twitter.com/PublicExtension/status/730434956376346624>
> 
> I would have gone straight to a proposal, but I think we can do some  
> bikeshedding about the name of `toggle`?
> 
> Another verb that could work is `invert`.
> 
> The `!` operator that does this is the negation operator, but I think  
> `negate` could sound to some like "make this false" rather than toggling.
> 
> Nate  
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> swift-evolution mailing list  
> swift-evolution@swift.org  
> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> swift-evolution mailing list  
> swift-evolution@swift.org  
> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> 
> --  
> Alejandro Martinez
> 
> > **[Alexito's World](https://alejandromp.com)**
> >
> > A world of coding 💻, by Alejandro Martinez
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> swift-evolution mailing list  
> swift-evolution@swift.org  
> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> swift-evolution mailing list  
> swift-evolution@swift.org  
> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> swift-evolution mailing list  
> swift-evolution@swift.org  
> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [January 20, 2018, 6:16am UTC](https://forums.swift.org/t/adding-toggle-to-bool/7414/20 "2018-01-20T06:16:25Z")

</div>

I agree that it's good to name things to fit the most common case (e.g. if `!isActive` is more commonly used than `isActive`, then the API would be better suited as `isInactive`). But in generally, I don't think it's a good idea to implement both the affirmation and a negation (i.e. having both `isActive` AND `isInactive` as part of an API). It just spams the namespace

[Next page](https://forums.swift.org/t/adding-toggle-to-bool/7414.md?page=2)
