-
Notifications
You must be signed in to change notification settings - Fork 207
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
Deprecate conditional operator ?/: #740
Comments
I'm very much in favour of doing something about the ternary operator. Now, before NNBD syntax becomes sprinkled all over existing codebases, seems like an ideal time to let dartfmt do the heavy lifting of a syntax change. The ternary operator is still unique in its syntax and I think it should be possible to come up with a reasonably clean replacement. That being said, using something based on if brings in lots of potential clashes with if statements, expressions and collection ifs. I think this is another corner of the language spec that is now proving to be kind of a dead end. It would be awesome to get if expressions. But collection-ifs will cause ambiguitiy. :( |
I'm not sure the ambiguity is that much of a problem for collection-ifs. Example: var x = [ if (test) 1 else 2 ]; Here it's ambiguous syntax, but the behaviors of the two options are indistinguishable. We'd obviously have to go through all the syntax corners to make sure there are no cases I have missed. |
Note: This issue is not for collection-if. Please discuss it at #729 or post a new issue. |
?
with mandatory else-part
The issue with collection-if here is that expressions occur in collections too, so there is some amount of syntactic overlap/ambiguity if expression-if starts using the same syntax. There is no need to change collection-if, and as stated this issue is not about doing so anyway, but changing expression-if interacts with collection-if, and that interaction needs to be discussed. (As stated above, I believe that interaction and ambiguity to be not-too-problematic). It's true that removing The options are really: <expression> ::= `if` `(` <expression> `)` <expression> `else` <expression> or <expression> ::= `if` `(` <expression> `)` <expression> (`else` <expression>)? Anything else will just be inconsistent. Since expressions must evaluate to a value or throw, omitting the else branch means that the omitted branch needs a default semantics.
The first one is what we currently do for The second one is adding Throwing means that it's effectively an error, and we should probably have gone with not allowing it anyway. Not practically useful. So, my vote would be on requiring an Ob-pedantry: The |
I understand.
I would also vote on requiring else branch. (The first title of this issue was "... mandatory else-part") A syntax clean braking from if-statement and collection if is also attractive for me. |
@tatumizer |
I prefer |
Using I can see the reasoning for "optional arguments". A non-else-condition in an optional argument position could mean not passing the argument, but currently it would only work for named optional parameters. Omitting a positional parameter would shift the remaining arguments up, which is not sound in general. If we had a way to omit optional arguments in the middle of a positional parameter chain, then it would fit better (not shift later arguments up, but still trigger default value in the function). That's a different feature. |
Is comma operator in JavaScript or C populer for modern language like Dart? |
What do you think about |
@tatumizer As I mentioned in the Motivation, non-affinity between NNBD and
|
Generally speaking, I totally agree with you. |
Just thought I'd give some thoughts from the perspective of an educator. A ton of languages have the ternary operator with the same syntax as in dart; There are a few other alternatives from other languages that might be worth exploring. MySQL uses the |
@tensor-programming
|
@Cat-sushi I do agree with you from the standpoint of #376. If the subscript syntax becomes at odds with the ternary then learning to use the subscript properly would pose a challenge for newer devs. |
@Cat-sushi it worth noting that ternary operator wouldn't removed from the Dart language immediately as it will break a lot of code. So this issue wouldn't help to resolve the problem |
Null-aware subscripting is now resolved. |
@vanesyan Dart will introduce a migration tool for NNBD, and I'm proposing the migration tool converting ternary operations into new syntax. |
The language team has decided that subscripting will have form of |
I see the issue is long closed. Just wanted to add that with a |
Motivation
NNBD, which is introducing many notations with
?
, makes the ternary operator?
/:
an eyesore more and more. For example, when null aware subscribing operation has shape ofa?[b]
, then{a?[b]:c}
is ambiguous at least mentally. The same thing goes for invocation{a?(b):c}
.In addition some proposals such as #357 might make the situation worse and worse.
T?
,??
,...?
, and so on are OK, but ...Note
This issue is derived from #376 and #729.
This issue aims to alternate the ternaly operation
a ? b : c
.This issue doesn't treat collection-if or if-statement, directly.
Candidates of syntax
if(a) b else c
// 1if(a, b, c)
// 2if(a : b : c)
// 3and more
The text was updated successfully, but these errors were encountered: