Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-13 Thread Eneko Alonso via swift-evolution
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 
>  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 
>>  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
>>  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(execute closure: () throws -> T) rethrows -> T? {
>>>   if self { return try closure() }
>>>   return nil
>>> }
>>> 
>>> @discardableResult
>>> func whenFalse(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
>>>  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
>> 

Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-13 Thread David Sweeris via swift-evolution

> On Jan 11, 2018, at 22:15, Chris Eidhof via swift-evolution 
>  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`?

“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___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-13 Thread Erica Sadun via swift-evolution
On Jan 13, 2018, at 12:06 PM, Karl Wagner via swift-evolution 
 wrote:
> 
>> On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution 
>> > 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`.

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


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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-13 Thread Adrian Zubarev via swift-evolution
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 
 wrote:



On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution 
 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
 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(execute closure: () throws -> T) rethrows -> T? {
  if self { return try closure() }
  return nil
}

@discardableResult
func whenFalse(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
 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
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-13 Thread C. Keith Ray via swift-evolution
(I know isTrue is redundant. I only considered it for symmetry.)

> On Jan 13, 2018, at 11:09 AM, C. Keith Ray  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 
>> > wrote:
>> 
>> 
>> 
>>> On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution 
>>> > 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
>>> > 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(execute closure: () throws -> T) rethrows -> T? {
   if self { return try closure() }
   return nil
 }
 
 @discardableResult
 func whenFalse(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
 > 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 
>> 

Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-13 Thread C. Keith Ray via swift-evolution
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 
>  wrote:
> 
> 
> 
>> On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution 
>> > 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
>>  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(execute closure: () throws -> T) rethrows -> T? {
>>>   if self { return try closure() }
>>>   return nil
>>> }
>>> 
>>> @discardableResult
>>> func whenFalse(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
>>>  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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-13 Thread Adrian Zubarev via swift-evolution
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 (razie...@gmail.com) schrieb:



> On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution 
>  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  
>  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(execute closure: () throws -> T) rethrows -> T? {  
>> if self { return try closure() }  
>> return nil  
>> }  
>>  
>> @discardableResult  
>> func whenFalse(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  
>>  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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-13 Thread Karl Wagner via swift-evolution


> On 12. Jan 2018, at 20:54, Alejandro Martinez via swift-evolution 
>  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
>  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(execute closure: () throws -> T) rethrows -> T? {
>>if self { return try closure() }
>>return nil
>>  }
>> 
>>  @discardableResult
>>  func whenFalse(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
>>  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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-13 Thread Chris Eidhof via swift-evolution
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 :P
> >
> > On Fri, Jan 12, 2018 at 10:13 AM, Adrian Zubarev via swift-evolution
> >  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(execute closure: () throws -> T) rethrows -> T? {
> >>if self { return try closure() }
> >>return nil
> >>  }
> >>
> >>  @discardableResult
> >>  func whenFalse(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
> >>  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
___
swift-evolution mailing list

Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-12 Thread Anders Kierulf via swift-evolution
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 
>  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
>  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(execute closure: () throws -> T) rethrows -> T? {
>>if self { return try closure() }
>>return nil
>>  }
>> 
>>  @discardableResult
>>  func whenFalse(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
>>  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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-12 Thread Adrian Zubarev via swift-evolution
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 (alexi...@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  
 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(execute closure: () throws -> T) rethrows -> T? {  
> if self { return try closure() }  
> return nil  
> }  
>  
> @discardableResult  
> func whenFalse(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  
>  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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-12 Thread Alejandro Martinez via swift-evolution
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
 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(execute closure: () throws -> T) rethrows -> T? {
> if self { return try closure() }
> return nil
>   }
>
>   @discardableResult
>   func whenFalse(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
>  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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-12 Thread Ben Cohen via swift-evolution
+1 for toggle, seems the clear winner for me.

> On Jan 12, 2018, at 8:29 AM, Cheyo Jimenez via swift-evolution 
>  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(_ 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.


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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-12 Thread Cheyo Jimenez via swift-evolution


> On Jan 12, 2018, at 12:14 AM, Nate Cook via swift-evolution 
>  wrote:
> 
> 
>> On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution 
>>  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`.

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. 



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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-12 Thread Xiaodi Wu via swift-evolution
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
>
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-12 Thread David Hart via swift-evolution


> On 12 Jan 2018, at 07:15, Chris Eidhof via swift-evolution 
>  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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-12 Thread Adrian Zubarev via swift-evolution
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(execute closure: () throws -> T) rethrows -> T? {
    if self { return try closure() }
    return nil
  }

  @discardableResult
  func whenFalse(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 
 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


Re: [swift-evolution] [pitch] adding toggle to Bool

2018-01-12 Thread Nate Cook via swift-evolution

> On Jan 12, 2018, at 12:15 AM, Chris Eidhof via swift-evolution 
>  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