Coder Social home page Coder Social logo

Comments (10)

mateusfccp avatar mateusfccp commented on June 25, 2024 3

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

from language.

lrhn avatar lrhn commented on June 25, 2024 2

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.

from language.

Mike278 avatar Mike278 commented on June 25, 2024 2

You could also write your example as:

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

from language.

tilucasoli avatar tilucasoli commented on June 25, 2024 2

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

from language.

Wdestroier avatar Wdestroier commented on June 25, 2024 1

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.

from language.

mraleph avatar mraleph commented on June 25, 2024 1

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.

from language.

mateusfccp avatar mateusfccp commented on June 25, 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
}

from language.

MohiuddinM avatar MohiuddinM commented on June 25, 2024

@tilucasoli wouldn't the pythonic way be better?

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

from language.

MohiuddinM avatar MohiuddinM commented on June 25, 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.

from language.

lrhn avatar lrhn commented on June 25, 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.

from language.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.