-
Notifications
You must be signed in to change notification settings - Fork 213
Feature Request: Add .toggle() extension for bool in Dart #4257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is not possible with how It would be possible to do it if we introduced a wrapper-like object (something similar to what Java does with his Something like this: void main() {
final boolean = True;
print(boolean);
boolean.toggle();
print(boolean);
if (boolean == false) {
print("Boolean is false");
}
if (false == boolean) {
print("false is Boolean");
}
}
/// A stateful wrapper to `bool`.
final class Bool {
Bool._(this._value);
// The internal boolean value.
bool _value;
/// Toggles the boolean value.
void toggle() {
_value = !_value;
}
@override
String toString() => '$_value';
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
} else if (other is bool) {
return _value == other;
} else if (other is Bool) {
return _value == other._value;
} else {
return false;
}
}
int get hashCode => _value.hashCode;
}
/// The true [Bool] value.
// ignore: non_constant_identifier_names
get True => Bool._(true);
/// The false [Bool] value.
// ignore: non_constant_identifier_names
get False => Bool._(false); Note that there's a limitation on this approach, in that This happens, because there's no way to define the "truthiness" of an object, so we rely on |
In swift, the implementation is extension Bool {
/// Useful when operating on long chains:
/// myVar.prop1.prop2.enabled.toggle()
mutating func toggle() {
self = !self
}
}
//
myVar.prop1.prop2.enabled.toggle(); There's nothing like this in dart. myVar.prop1.prop2.enabled.={!this}; |
Dart doesn't have reference parameters, so you cannot abstract over variables, only values. Assume that Dart does get reference parameters (#1911), say using a prefix void toggle (bool &v) { x = !x;} and possibly extension BoolToggle on bool &v {
void toggle() {
v= !v;
}
} (I'd like to allow extension Until then, no |
As others have said, Dart doesn't have first-class lvalues, so it's not possible to implement this an extension and it doesn't seem to warrant a language feature. I'm going to go ahead and close this, but I appreciate you filing the issue and kicking off this discussion. :) |
Appreciate the discussion and insights from everyone. Given Dart's lack of first-class lvalues and reference parameters, I see why .toggle() as an extension method isn't feasible. While it would have been a syntactic improvement for state management, implementing it would require deeper language changes that likely don’t justify the cost. The suggested wrapper-based approach is interesting but comes with trade-offs. I also see potential if Dart introduces reference parameters in the future. Thanks again for the constructive discussion. |
In Swift, the Bool type has a built-in
.toggle()
method that flips the value betweentrue
andfalse
. It simplifies boolean state management and avoids repetitive expressions like:isEnabled = !isEnabled;
I propose adding a
.toggle()
method for bool in Dart, which would allow:Use Case
This feature would improve readability and reduce redundancy, especially in stateful applications where toggling booleans is common.
Proposed Implementation
A simple method can be added to
bool
class in the Dart SDK:Adding
.toggle()
to bool would improve developer experience, reduce verbosity, and align Dart with other modern programming languages.References
Swift's
toggle()
method: hereThe text was updated successfully, but these errors were encountered: