-
Notifications
You must be signed in to change notification settings - Fork 202
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
More Concise Syntax for Multiple Conditions #3662
Comments
Would an extension method help? main() {
var a = 7;
final isNumberGreaterThanFive = a > 5;
final isNumberLesserThanTen = a < 10;
if (isNumberGreaterThanFive.and(isNumberLesserThanTen)) {}
}
extension NamedAndOperator on bool {
bool and(bool other) => this && other;
} Or is it more of a formatter feature request? Possibly related to #2885. |
So this is making And it only works in Also uses syntax that could potentially be used for something else, for something that already has a working syntax. Going to be a hard sell. Benefits so small the opportunity costs alone outweigh them. |
This suggests replacing an obvious and well understood thing (logical and I think this is not just a hard sell - it is pretty much a no-go. If condition is so complicated that it becomes hard to read - then the best strategy is to split it into variables instead. |
If you just want to have each condition in one line, you can always force the split by using if (condition1 && //
condition2 &&
condition3) {
// Do something
} |
You could also write your example as: if (a case > 5 && < 10) {
// some code...
} |
@tilucasoli wouldn't the pythonic way be better?
|
I think what @tilucasoli wants is the formatting because in Dart the commas are also used to nicely format across multiple lines. But I think formatting does not require any language changes and can be made to work with |
I concur with your perspective on the use of the comma void main() {
Bar a = Foo();
if (
a is Foo,
a?.name == 'Bar' // Conditional check
) {
// Inside this block, 'a' is promoted to 'Foo'
// and can be used as such.
print(a.name); // This is now safe and type-checked.
}
}
class Bar {}
class Foo extends Bar {
String? name;
}
In this scenario, the comma operator allows a easier control flow. Unlike the |
You can do the same with void main() {
Bar a = Foo();
if (a case Foo(name: 'Bar')) {
// Inside this block, 'a' is promoted to 'Foo'
// and can be used as such.
print(a.name); // This is now safe and type-checked.
}
}
class Bar {}
class Foo extends Bar {
String? name;
}
|
The If the comma operator would evaluate both conditions, even if the first was false, then it's less useful and efficient than if (
a is Foo &&
a.name == 'Bar' // No null-aware access needed, first condition promotes.
) {
// Inside this block, 'a' is promoted to 'Foo'
// and can be used as such. (Yep, works!)
print(a.name); // This is now safe and type-checked.
} The I see no win for the comma, sorry. |
I'm going to close this out because I don't see it working out. |
Proposal:
In Dart, the current syntax for evaluating multiple conditions within an if statement requires the use of logical operators such as
&&
or||
. This approach, while functional, can sometimes lead to verbose and less readable code, especially when dealing with a large number of conditions.Current Syntax:
Proposed Syntax:
To streamline the process and improve code readability, I propose introducing a new syntax that allows for a more concise representation of multiple conditions within an if statement. This new syntax would involve listing conditions separated by commas.
Example of Proposed Syntax:
The text was updated successfully, but these errors were encountered: