# SE-0199: Adding toggle method to Bool

**URL:** <https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841>\
**Category:** Proposal Reviews\
**Created:** [February 12, 2018, 10:11pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841 "2018-02-12T22:11:34Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![palimondo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/palimondo/32/1144_2.png) [@palimondo](https://forums.swift.org/u/palimondo)\
**Post date:** [February 13, 2018, 8:29pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/21 "2018-02-13T20:29:55Z")

</div>

+1, the proposal provides sweet little utility with unambiguous name that cuts boilerplate in half of more (see Optionals chaining examples above). That is always a win in my book.

---

<div class="post-metadata">

**Author:** ![Rod\_Brown](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rod_brown/32/425_2.png) [@Rod\_Brown](https://forums.swift.org/u/Rod_Brown)\
**Post date:** [February 13, 2018, 9:01pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/22 "2018-02-13T21:01:34Z")

</div>

What is your evaluation of the proposal?  
Mildly +1.

The only reason I say mild is I can see the potential for this to be misused by devs who toggle and assume their state is consistent with the reason they’re toggling it eg from a UI component. I would recommend devs be explicit where possible rather than use functionality like this and risk inconsistent state propagating. I can definitely see the use case, though.

Is the problem being addressed significant enough to warrant a change to Swift?  
It can be tough to deal with deeply nested Booleans with optional chaining without duplicate boilerplate. I’m not 100% sure that something this convenient is wise, though...

Does this proposal fit well with the feel and direction of Swift?  
It’s definitely a functional and useful tweak. Though we did evaluate an array[ifExists: index] bounds checked convenience to be a bad idea, if I recall...

If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?  
N/A

How much effort did you put into your review? A glance, a quick reading, or an in-depth study?  
Quick reading.

---

<div class="post-metadata">

**Author:** ![QuinceyMorris](https://avatars.discourse-cdn.com/v4/letter/q/d26b3c/32.png) [@QuinceyMorris](https://forums.swift.org/u/QuinceyMorris)\
**Post date:** [February 13, 2018, 9:17pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/23 "2018-02-13T21:17:54Z")

</div>

+1 to `.flip()` from me. "Toggle" as a verb sounds a bit Americanized. "Flip" seems more vivid.

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [February 13, 2018, 10:39pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/24 "2018-02-13T22:39:13Z")

</div>

+1 from me.

I mildly prefer the name `.invert()`, as it's less skeuomorphic.

---

<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:** [February 13, 2018, 10:49pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/25 "2018-02-13T22:49:14Z")

</div>

> [@QuinceyMorris](#):
>
> “Toggle” as a verb sounds a bit Americanized.

Agree. I was even thinking that `.switch()` would be more universally understood as it relates to programing.

---

<div class="post-metadata">

**Author:** ![lancep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancep/32/1509_2.png) [@lancep](https://forums.swift.org/u/lancep)\
**Post date:** [February 13, 2018, 11:38pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/26 "2018-02-13T23:38:21Z")

</div>

I'm in favor of this change.

I don't think comparing `toggle()` to `++` is valid for the same reasons Chris mentioned.The name `toggle` is my preference. `flip` sounds too bit-wisey to me. `invert` is ok but less clear than `toggle`. `switch` already means something else in the language and should not be overloaded in my opinion.

---

<div class="post-metadata">

**Author:** ![kiel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kiel/32/1392_2.png) [@kiel](https://forums.swift.org/u/kiel)\
**Post date:** [February 14, 2018, 12:28am UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/27 "2018-02-14T00:28:45Z")

</div>

I'm generally in favour of the proposal because of the 'argument from random usefulness' and the 'argument from optional chaining'.

I'm wondering out loud here: could this proposal encourage developers to write less meaningful code?

For example, if I had a `LightControl` type, I wouldn't expose the boolean property modelling the state because it is more meaningful in the model's domain to say:

```swift
var control = LightControl()
control.switch()
print("the light is \(control.isOn ? "On" : "Off" )")

```

...rather than:

```swift
var control = LightControl()
control.isOn.toggle()
print("the light is \(control.isOn ? "On" : "Off" )")

```

The difference seems pedantic, which makes me inclined to think there's less potential harm to code quality and readability. But is that accountable to the quality of the example or the invalidity of the concern for code quality?

It's also hard to think of other occasions where you want to toggle a boolean value rather than setting booleans to a particular state in bulk (eg: mark as unread/read).

Maybe someone smarter than me can point out the error of my concerns?

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [February 14, 2018, 12:51am UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/28 "2018-02-14T00:51:36Z")

</div>

+0.1, though I’d prefer `toggled` for a non-mutating and `toggle` for mutating.

---

<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:** [February 14, 2018, 1:05am UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/29 "2018-02-14T01:05:44Z")

</div>

> [@kiel](#):
>
> For example, if I had a LightControl type, I wouldn’t expose the boolean property modelling the state because it is more meaningful in the model’s domain to say:
> 
> ```swift
> var control = LightControl()
> control.switch()
> print("the light is \(control.isOn ? "On" : "Off" )")
> 
> ```

Yes! Thank you; that is more or less what I have been trying to say. For a light it makes more sense for the programmer to decide on the best name of the method. For a light `.switch` makes more sense. For something like `.isMovingFoward` then `.revert()`; For `.isUpside` then perhaps `.invert()` makes more sense for my application, etc, etc.

---

<div class="post-metadata">

**Author:** ![jawbroken](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jawbroken](https://forums.swift.org/u/jawbroken)\
**Post date:** [February 14, 2018, 1:18am UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/30 "2018-02-14T01:18:21Z")

</div>

This sounds like an argument for providing no functionality at all in the standard library, because it might be used in a context where the name isn't optimal. This is a method on `Bool`, not on LightControl, so the name only has to make sense for `Bool`, not for any type that may use a `Bool` internally. You can, of course, implement a method on `LightControl`, with whatever name you prefer, and then call `toggle()` on the `Bool` representing its internal state.

---

<div class="post-metadata">

**Author:** ![owensd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/owensd/32/8934_2.png) [@owensd](https://forums.swift.org/u/owensd)\
**Post date:** [February 14, 2018, 5:32pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/31 "2018-02-14T17:32:38Z")

</div>

+1, with notes below:

I don't think the proposal lays out a convincing argument that it should be included, and it provides very little in terms of use cases that would be significantly improved.

However, this post: [SE-0199: Adding toggle method to Bool - #16 by Ben\_Cohen](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/16) does a great job in laying out some of the benefits.

My personal thoughts on the matter: I really enjoy when standard libraries do things that help in the pragmatic case. I think this is one of those.

---

<div class="post-metadata">

**Author:** ![Ponyboy47](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ponyboy47/32/2605_2.png) [@Ponyboy47](https://forums.swift.org/u/Ponyboy47)\
**Post date:** [February 14, 2018, 6:43pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/32 "2018-02-14T18:43:53Z")

</div>

+1

I was of the opinion that this wasn't a useful enough addition to add to the standard library. It certainly wan't harmful, but @ben-cohen's response with the examples of its usage has swayed me to be in favor of it.

I would prefer `.toggled()` be added as a non-mutating version as well though and it seems like a minor and harmless thing to add to this proposal.

I also agree that the name should be toggle, of course, I am an American so that might be why I like it ;)

---

<div class="post-metadata">

**Author:** ![cal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cal/32/35557_2.png) [@cal](https://forums.swift.org/u/cal)\
**Post date:** [February 14, 2018, 7:19pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/33 "2018-02-14T19:19:22Z")

</div>

+1

I think this solves a real problem, and is a welcome addition. There's precedence for methods like this being included in the standard library (see: `Int.negate`), so this seems perfectly reasonable.

---

<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:** [February 14, 2018, 7:26pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/34 "2018-02-14T19:26:02Z")

</div>

> [@cal](#):
>
> There’s precedence for methods like this being included in the standard library (see: Int.inverted), so this seems perfectly reasonable.

Perhaps it was renamed to `.negate()` [negate() | Apple Developer Documentation](https://developer.apple.com/documentation/swift/int/2884785-negate)

---

<div class="post-metadata">

**Author:** ![cal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cal/32/35557_2.png) [@cal](https://forums.swift.org/u/cal)\
**Post date:** [February 14, 2018, 8:00pm UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/35 "2018-02-14T20:00:19Z")

</div>

> [@masters3d](#):
>
> Perhaps it was renamed to .negate()

My mistake, I meant `.negate()` -- corrected!

---

<div class="post-metadata">

**Author:** ![jawbroken](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jawbroken](https://forums.swift.org/u/jawbroken)\
**Post date:** [February 15, 2018, 12:24am UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/36 "2018-02-15T00:24:01Z")

</div>

I'm not sure what the point of having the `toggled()` non-mutating version would be. There is already an operator for this, and it wouldn't have the same advantage of reducing repetition on both sides of an assignment.

---

<div class="post-metadata">

**Author:** ![lancep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancep/32/1509_2.png) [@lancep](https://forums.swift.org/u/lancep)\
**Post date:** [February 15, 2018, 12:31am UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/37 "2018-02-15T00:31:20Z")

</div>

I write `someLong.chained.booleanExpression() == false` all the time because for long expressions, the `!` at the beginning can get lost easily. `toggled()` would "solve" that for me

---

<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:** [February 15, 2018, 12:34am UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/38 "2018-02-15T00:34:56Z")

</div>

To clarify my review manager note:

`toggled()` is a reimplementation of `!` as a function, and as such is _probably_ on the commonly rejected proposals list. I say probably because the reasoning for rejecting that particular variant isn't listed, but I expect we'd consider it covered by it.

Whether to add `toggled()` or not would also be a separate proposal that doesn't affect whether or not a mutating `toggle()` should be accepted, so if it weren't on the commonly-rejected list, it should be raised as a new pitch rather than discussed in this review.

---

<div class="post-metadata">

**Author:** ![bzamayo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bzamayo/32/764_2.png) [@bzamayo](https://forums.swift.org/u/bzamayo)\
**Post date:** [February 15, 2018, 12:39am UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/39 "2018-02-15T00:39:23Z")

</div>

I’m in favour of the concept but I frankly do not feel strongly either way on its inclusion, it blurs the line between being moderately useful and being too trivial to warrant its existence.

The proposed name `toggle()` does not feel appropriate. I think it implies that something else is happening, as if it’s a higher-level abstraction of some kind. In a GUI app context, the signature could get confused for a UI component action for example. Standard library API should be blandly named, `toggle()` isn’t ‘boring’ enough. `invert()` is my recommendation.

---

<div class="post-metadata">

**Author:** ![00buggy00](https://avatars.discourse-cdn.com/v4/letter/0/cab0a1/32.png) [@00buggy00](https://forums.swift.org/u/00buggy00)\
**Post date:** [February 15, 2018, 1:18am UTC](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841/40 "2018-02-15T01:18:37Z")

</div>

Great big **+1** from me--even if for just this example. It would really improve composition with optional chaining. Yes, I could just implement this myself, but it would be nice to not have to do so for each project (personally I could get plenty of good use out of this). Not terrible if it doesn't make it in though.

[Previous page](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841.md?page=1)

[Next page](https://forums.swift.org/t/se-0199-adding-toggle-method-to-bool/9841.md?page=3)
