Skip to content
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

Closed
tilucasoli opened this issue Mar 19, 2024 · 11 comments
Closed

More Concise Syntax for Multiple Conditions #3662

tilucasoli opened this issue Mar 19, 2024 · 11 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@tilucasoli
Copy link

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:

if (a > 5 && a < 10) {
 // some code...
}

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:

if (
 a > 5,
 a < 10,
) {
 // some code...
}
@tilucasoli tilucasoli added the feature Proposed language feature that solves one or more problems label Mar 19, 2024
@Wdestroier
Copy link

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.
I wrote in the comments to (possibly) allow an optional comma before closing the if statement.

@lrhn
Copy link
Member

lrhn commented Mar 19, 2024

So this is making , mean &&, so you have two ways to write the same thing.

And it only works in if and while conditions, maybe for?
Probably not a good fit for conditional expression or when conditions which are not parenthesized.

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.

@mraleph
Copy link
Member

mraleph commented Mar 19, 2024

This suggests replacing an obvious and well understood thing (logical and &&) with a unobvious thing (comma ,). What does comma even mean? Is it logical and? Is it logical or? Is it execute-all-expressions-and-take-just-the-last-result like in JavaScript or C++?

I think this is not just a hard sell - it is pretty much a no-go. && is clearly more readable then ,.

If condition is so complicated that it becomes hard to read - then the best strategy is to split it into variables instead.

@mateusfccp
Copy link
Contributor

mateusfccp commented Mar 19, 2024

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
}

@Mike278
Copy link

Mike278 commented Mar 19, 2024

You could also write your example as:

if (a case > 5 && < 10) {
 // some code...
}

@MohiuddinM
Copy link

@tilucasoli wouldn't the pythonic way be better?

if (5 < a < 10) {
 // some code...
}

@MohiuddinM
Copy link

MohiuddinM commented Mar 20, 2024

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 && and || too just like commas.

@tilucasoli
Copy link
Author

tilucasoli commented Mar 26, 2024

I concur with your perspective on the use of the comma , as an alternative to the && operator. But in the case that the comma operator can be used to casting or unwrap optional values. Let's consider the following example:

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 && operator, which evaluates both conditions together, the comma operator evaluates each condition separately. This means that after the type check (a is Foo), the variable a is promoted to the type Foo and not necesseraly need to be created a new if statement to do check the condition.

@mraleph

@mateusfccp
Copy link
Contributor

mateusfccp commented Mar 26, 2024

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

@lrhn
Copy link
Member

lrhn commented Apr 5, 2024

In this scenario, the comma operator allows a easier control flow. Unlike the && operator, which evaluates both conditions together, the comma operator evaluates each condition separately.

The && operator evaluates the conditions sequentially, one before the other.
Dart is single-threaded, so no matter what the comma operator does, it will also evaluate one expression before the other (unless they're async, then they can technically be interleaved, but you'll have to put the awaits somewhere).

If the comma operator would evaluate both conditions, even if the first was false, then it's less useful and efficient than &&.
And it won't get the promotion that allows you to write:

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 && does at least the same promotion as you're suggesting for the comma operator here.

I see no win for the comma, sorry.

@munificent
Copy link
Member

I'm going to close this out because I don't see it working out.

@munificent munificent closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

8 participants