At the moment Dart has ternary operator that requires "else" part.
In the cases when I need something like this
var s1 = 3;
s1 = s1 > 3 ? 100500 : s1
": s1" is redunant.
Or I should use "if"
var s1 = 3;
if(s1 > 3){
s1 = 100500;
}
I think It will be useful to have another one aproach to do this, something like this:
var s1 = 3;
s1 ?= s1 > 3 : 100500;
var s2 = 3;
s2 ?= s2 > 3 : 100500 + 56 * 78;
If the condition is false than all expression ignors. And the old value will not change.
This is something like collection "if" but witout collection.
var s1 = 3;
if (s1 > 3) s1 = 10050;
is more readable imo.
Ok, but these
var s1 = 3;
if (s1 == null) s1 = 10050;
var s1 = 3;
var s2 = 5;
if (s1 == null) s1 = s2;
md5-d0000d4257a3695c2d586ffec1c0e04c
var s1 = 3;
var s2 = 5;
if (s1 > 3) {
s1 = 100500;
} else {
s1 = s2;
}
md5-f91ac21a0ae2ee60ca51121217f04f02
var s1 = 3;
s1 ??= 10050;
md5-d0000d4257a3695c2d586ffec1c0e04c
var s1 = 3;
var s2 = 5;
s1 = s1 ?? s2;
md5-d0000d4257a3695c2d586ffec1c0e04c
var s1 = 3;
var s2 = 5;
s1 = s1 > 3 ? 100500 : s2;
But for some reasons they exist ...
Which is why I think dart should support if expressions (I think there's another issue for it). These operators, though short, severely affect readibility.
@vr19860507 :
Operator ??= is an afterthought. It comes as a natural consequence of having an operator ??. In dart, whenever you have a binary operator op, you ALSO have op=. Adding op= without adding op would be inconsistent and not even very useful. Consider a case where you pass an optional parameter to a function. E.g. foo(a>b?a:NOTHING). It's similar to the assignment, but without =. There are other cases where you where NOTHING would be quite handy (see the issues I listed earlier).
So, it all boils down to the question of how to express NOTHING.
There are 3 possibilities:
1) symbol e.g. :-)
2) noun (TBD)
3) verb (TBD)
The first variant (symbol) is not viable : 1) dart is already running out of symbols 2) it's difficult to choose a symbol that stands for "nothing" without causing great confusion among users
The second variant (noun) leads to the following problem. Suppose we choose a noun sepulka. It looks like a value, so it should have a type. What is sepulka's type? And what if we use it in the expression? E.g. what is the value of sepulka*2? Or sin(sepulka)? What is the type of this value? Clearly, a noun is a non-starter.
Which leaves us with the only logical choice: a verb. Let's try "ignore":
s1 = s1 > 3 ? 100500 : ignore;
foo(optionalParameter:s1 > 3 ? 100500 : ignore);
s1 = (s1 > 3 ? 100500 : ignore)+50;
Since it's a verb, we don't need to define a type, but need to define an action. The action is simple: stop the evaluation of the expression and behave as if this expression was not even there. What remains is to come up with the list of contexts where ignorable expressions make sense (elements of list/map literals, passing optional parameters, etc.)
Sounds logical, I like the idea about "ignore"
Most helpful comment
is more readable imo.