Skip to content
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

Logical compound assignment operators &&= and ||= #23

Open
eernstg opened this issue Sep 7, 2018 · 5 comments
Open

Logical compound assignment operators &&= and ||= #23

eernstg opened this issue Sep 7, 2018 · 5 comments
Labels
request Requests to resolve a particular developer problem

Comments

@eernstg
Copy link
Member

eernstg commented Sep 7, 2018

Cf. issues dart-lang/sdk#26996, meta, and dart-lang/sdk#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.

@mit-mit mit-mit added feature Proposed language feature that solves one or more problems request Requests to resolve a particular developer problem and removed feature Proposed language feature that solves one or more problems enhancement labels Oct 16, 2018
@thosakwe
Copy link

The following prints out true:

void main() {
  var a = false;
  a |= true;
  print(a);
}

So maybe it's already implemented?

@eernstg
Copy link
Member Author

eernstg commented Aug 12, 2019

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.

@thosakwe
Copy link

Ah, I see.

@marcglasberg
Copy link

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

@eernstg
Copy link
Member Author

eernstg commented Nov 27, 2020

It is not abandoned, cf. #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!');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

4 participants