Comments (10)
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.
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.
You could also write your example as:
if (a case > 5 && < 10) {
// some code...
}
from language.
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.
from language.
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.
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.
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.
@tilucasoli wouldn't the pythonic way be better?
if (5 < a < 10) {
// some code...
}
from language.
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.
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 await
s 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)
- Static type metadata guided shorthands and features HOT 4
- No macro.TypeDeclarationImpl for `dynamic` HOT 4
- Adoption of functional programming in the Dart language HOT 2
- Enhancing `library` and `part of` Declarations in Dart HOT 1
- Create the type `Monad` HOT 4
- Allow `import` and `part` to have wildcard path to import every lib in specific directory HOT 1
- Adding conditional imports/parts with macros. HOT 2
- Consider changing the context for the operand of `throw` to `Object`. HOT 3
- Allow statically known class fields to be used in switch statements HOT 4
- could support static extension? HOT 2
- Macro - Augment class with generic HOT 7
- Macro annotation information in yaml files next steps HOT 2
- Network paths don't work anymore with version 3.4.0 HOT 2
- "Merge to source" for macros that only need Phase 3 HOT 22
- Follow up on "spawn isolate from kernel blob" implementation
- Allow pattern `case` conditions in `while` statements. HOT 2
- Unable to call super method on an object. HOT 4
- Add syntactic sugar for package imports HOT 2
- Allow recursive extension type HOT 1
- Sub-package auto-export
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from language.