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

Dart is missing &&= and ||= operators #25264

Closed
Hixie opened this issue Dec 15, 2015 · 3 comments
Closed

Dart is missing &&= and ||= operators #25264

Hixie opened this issue Dec 15, 2015 · 3 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@Hixie
Copy link
Contributor

Hixie commented Dec 15, 2015

It's really useful to be able to write code like:

    result.canTap ||= onCanTap != null;
    result.canDoubleTap ||= onDoubleTap != null;
    result.canLongPress ||= onLongPress != null;

...but currently we can't because ||= and &&= don't work in Dart. |= and &= work, but | and & aren't implemented by bool so that doesn't help here.

@lrhn lrhn added type-enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Dec 16, 2015
@lrhn
Copy link
Member

lrhn commented Dec 16, 2015

I agree that it would be nice.

I think ||= and &&= would have slightly different semantics than other op= assignments. Those effectively do lhs = lhs op expr. In the case of ||= and &&= you probably don't even want to do the assignment if it is already true/false, so the translation should be something like:

lhs &&= expr   ==>   let v = lhs in (v ? lhs = expr : v)
lhs ||= expr   ==>   let v = lhs in (!v ? lhs = expr : v)

For non-short-circuit boolean operators, see #1080
Adding & and | to bool, like in Java and C++, was voted down at the time (the change is miniscule, it's two one-line functions in the core library, but the spec might want to make them compile-time constants as well).

@floitschG
Copy link
Contributor

Fwiw we already opened that can of worms with ??=. Since that one already exists we should just allow ||= and &&=.

@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
@munificent munificent self-assigned this Aug 17, 2016
@munificent munificent added the closed-duplicate Closed in favor of an existing report label Dec 10, 2016
@munificent
Copy link
Member

Tracked by: #26996. I know this one's older, but that one has more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants