Cf. issues https://github.com/dart-lang/sdk/issues/26996, meta, and https://github.com/dart-lang/sdk/issues/26997, analyzer, (there are others, but they should probably be replaced by a single front-end issue), the compound assignment operators &&= and ||= have been in the pipeline for a full specification and an implementation for a long time.
We ought to find a placement for this task in terms of milestones and priorities, or drop it.
The following prints out true:
void main() {
var a = false;
a |= true;
print(a);
}
So maybe it's already implemented?
No, that's a slightly different thing: The statement a |= true uses the operator | on bool, which is a regular operator. So expression evaluation for e1 | e2 is a regular instance method invocation. In particular, e2 _will_ be evaluated even in the case where e1 evaluates to true, and the same property holds for a |= e2 because it's defined in terms of an invocation of |.
This matters in practice, because you can use done ||= doWork() to skip the execution of doWork() in the case where done is already true.
Ah, I see.
Hey there, was this abandoned?
If I have this:
onPressed: doSomething,
I'd like to be able to do:
onPressed: doSomething && doSomethingElse || doSomethingElseEntirely,
By defining something like:
extension FunctionExtension on bool Function(String) {
bool Function(String) operator &&=(bool Function(String) other) =>
(String value) => this(value) || other(value);
bool Function(String) ||=(bool Function(String) other) =>
(String value) => this(value) && other(value);
}
It is not abandoned, cf. https://github.com/dart-lang/language/issues/1077. However, you cannot declare an instance operator for any compound assignment (+=, *=, etc.), and also not for && or || (because evaluation of e1 && e2 is different from a method invocation). But you can do this:
typedef F = bool Function(String);
extension LiftBool on F {
F operator &(F other) => (s) => this(s) && other(s);
F operator |(F other) => (s) => this(s) || other(s);
}
bool succeed(String s) { print('Succeed: $s'); return true; }
bool fail(String s) { print('Fail: $s'); return false; }
bool notReached(String s) => throw 'Not reached';
void main() {
(succeed & succeed | notReached)('Hello');
(fail & notReached | succeed)('world!');
}
Most helpful comment
No, that's a slightly different thing: The statement
a |= trueuses the operator|onbool, which is a regular operator. So expression evaluation fore1 | e2is a regular instance method invocation. In particular,e2_will_ be evaluated even in the case wheree1evaluates totrue, and the same property holds fora |= e2because it's defined in terms of an invocation of|.This matters in practice, because you can use
done ||= doWork()to skip the execution ofdoWork()in the case wheredoneis already true.