I tried to see if the operator resolution rules would allow two overloads for !.
The existing one, (Bool) -> Bool
A new mutating varient, (inout Bool) -> Void.
Unfortunately, I couldn't get the second overload to be called. Oddly, I didn't get an ambiguous operator error. I made a toy MyBool type to let me play with the operators:
I concur. I feel that .isTrue and .isFalse are obsoleted by the type Bool itself.
I don't feel strongly about adding or leaving out the toggle() and toggled() functions. invert() and inverted() don't make sense in my head, at least. I can't think of a situation where I'd find it obvious.
It feels like bad heritage from C, and it probably should be removed from Swift
I have to disagree here. ! is understood in many programming languages to represent negation. I feel like removing it now would introduce too much confusion and churn.
! does not provide any unique functionality, as it is redundant to “== false”
Like my reply above, every language that I have used has provided a way to do negation as an operator. It offers a concise way of expressing negation.
I did define toggle myself some time ago as I found it helpful.
For other methods though I believe it’s simpler to leave it to == false and avoid adding more code for options that don’t add readability. I like using prefix ! on values and == false on expressions
Glad to know I'm not the only one! I always write it like I say it, and you're right, that ! is especially hard to see when you're tired. == false makes it much more apparent.
I once thought about proposing something like the following:
var isEmpty<!>isNotEmpty: Bool
Where the compiler automatically inverts the logic when ‘isNotEmpty’ is used. This way the ! operator is not required anymore. To be honest, I’m not sure if it’s a good idea, but since we’re talking about it, I just thought I’d throw it out there.
If we’re talking about properties, I guess the compiler could generate a computed property:
So this:
var isEmpty<!>isNotEmpty: Bool
Translates to this:
var isEmpty: Bool
var isNotEmpty: Bool {
get { return !isEmpty }
set { isEmpty = !newValue }
}
I also meant this to be supported for local variables, but that might be more difficult to implement.
Regarding language support, this actually supports all languages. It's just the <!> (strawman syntax) that tells the compiler that the name of the inverted variable follows after it.
If this is used for every Bool, it would improve readability where they are used. I'm just not sure that it's worth the tradeoff of specifying two names for every Bool. Especially since I actually have no problems with the ! operator myself.
variable_decl ::= "var" "<!>" ":" ... generates two boolean var declarations.
so
var foobar<!>barfoo: Bool
generates something like:
var foobar : Bool
var barfoo : Bool {
get { return !foobar }
set { foobar = !newValue }
}
Though in practice, a programmer would likely choose names like (isEmpty, isNotEmpty) instead of (foobar, barfoo).
Not a bad suggestion, but it's not something I'd recommend adding to Swift.
Elsewhere, someone may have used Ruby's metaprogramming facilities to do this, though I haven't heard of anyone doing it. One could even implement a macro in C.
I’m sorry, but that’s ridiculous. Swift already has hundreds of “privileged” English properties and functions. Adding one more to Bool changes nothing. Users can still add whatever non-English identifiers they want.
Again, the idea is not to just insert 'Not' into the identifier. I thought of something like that, but also concluded that this was not suitable for people programming in other languages. The proposal lets you specify two Bool properties in one line, of which the second property is the inverse of the first. You can use any names you want for both properties, the word 'Not' has nothing to do with the proposal.
Some examples in other languages (my apologies if I butchered someones language):
var esteVacia<!>noEsteVacia: Bool
var estVide<!>nestPasVide: Bool
var istLeer<!>istNichtLeer: Bool
var isLeeg<!>isNietLeeg: Bool
Some other English examples:
var isValid<!>isInvalid: Bool
var isFast<!>isSlow: Bool
It's an interesting suggestion, although I'm not sure if it fits into Swift. In any case, this won't be in scope for the toggle proposal, so maybe we should branch off the discussion?
I agree, we are derailing the thread. I only mentioned it because people were talking about solutions for '... == false'. I'll create a new topic and we'll see whether there is any support for this.
Let's bring the discussion back to adding a toggle() function to Bool. I totally support this and 'toggle' as name seems fine to me.