Adding toggle to Bool


(Chris Eidhof) #1

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()

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:

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

···

--
Chris Eidhof


SE-0199: Adding toggle method to Bool
(Nate Cook) #2

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

···

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


(Chéyo Jiménez) #3

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`.

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 :frowning:

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


(Adrian Zubarev) #4

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 :wink:
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


(David Hart) #5

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()

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`?

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


(David Sweeris) #6

“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:

    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()

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`?


(Ben Cohen) #7

+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 {
  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.


(Xiaodi Wu) #8

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:

    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()

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`?

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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Alejandro Martinez) #9

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 :stuck_out_tongue:

···

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 :wink:

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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Alejandro Martinez
http://alejandromp.com


(Anders Kierulf) #10

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 {
      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 :stuck_out_tongue:

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 :wink:

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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Alejandro Martinez
http://alejandromp.com
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Adrian Zubarev) #11

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. :wink:

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 :stuck_out_tongue:

···

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 :wink:

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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Alejandro Martinez
http://alejandromp.com


(Chris Eidhof) #12

Thanks for all the feedback everyone, I just submitted a short proposal as
a PR: 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:

  extension String {
      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 :stuck_out_tongue:
>
> 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 :wink:
>>
>> 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
>>
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>
>
>
> --
> Alejandro Martinez
> http://alejandromp.com
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Chris Eidhof


(Karl) #13

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 :stuck_out_tongue:

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 :wink:

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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Alejandro Martinez
http://alejandromp.com
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Eneko Alonso) #14

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(;:wink: 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 {
     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 :stuck_out_tongue:

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 :wink:

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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Alejandro Martinez
http://alejandromp.com
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(C. Keith Ray) #15

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 :stuck_out_tongue:

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 :wink:

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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Alejandro Martinez
http://alejandromp.com
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
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


(C. Keith Ray) #16

(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 :stuck_out_tongue:

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() {
  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 :wink:

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:

  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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Erica Sadun) #17

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 :stuck_out_tongue:

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`.


(Adrian Zubarev) #18

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 :stuck_out_tongue:

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 :wink:

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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Alejandro Martinez
http://alejandromp.com
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Adrian Zubarev) #19

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 :stuck_out_tongue:

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 :wink:

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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Alejandro Martinez
http://alejandromp.com
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Alexander Momchilov) #20

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