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

Inverse nullish coalescing operator #2214

Closed
aabounegm opened this issue Apr 22, 2022 · 4 comments
Closed

Inverse nullish coalescing operator #2214

aabounegm opened this issue Apr 22, 2022 · 4 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@aabounegm
Copy link

We already have the useful nullish coalescing operator a ?? b that is semantically equivalent to a == null ? b : a. It would be great to also have an inverted null coalescing operator a !! b meaning a == null ? null : b. This would be useful in situations that require a non-null value for further computations, or otherwise be null.

For clarification, here is an actual example of code from a Flutter app I'm working on:

ListTile(
  leading: icon == null ? null : Icon(icon),
  title: target == null ? null : Text(target),
  subtitle: base == null ? null : Text(base),
  trailing: change == null ? null : const Icon(Icons.edit_rounded),
);

It would a lot less verbose if I could instead write it this way:

ListTile(
 leading: icon !! Icon(icon),
 title: target !! Text(target),
 subtitle: base !! Text(base),
 trailing: change !! const Icon(Icons.edit_rounded),
);

Similar to with ??, it would also be a dead_null_aware_expression to write a !! b when a is non-nullable and it would evaluate to b.
As for the expression type, it makes sense for it be T2?, assuming a has type T1? and b has type T2. Of course, this also involves promoting a to be non-nullable (T1) inside the b expression, like with the equivalent ternary operator expression.

It seems relatively simple to implement since it would just be a syntactic sugar that could be de-sugared in the compiler frontend.

@aabounegm aabounegm added the feature Proposed language feature that solves one or more problems label Apr 22, 2022
@mateusfccp
Copy link
Contributor

Duplicate of #361.

@aabounegm
Copy link
Author

Oh, sorry for not noticing it. I tried searching a lot, but it seems like GitHub doesn't support searching for special symbols, and the original issue didn't contain the keywords I looked for. I will close this issue.

But am I right that it is a small feature that shouldn't require much work? If so, can you please point me towards the correct location in the Dart SDK if I wanted to contribute? I tried walking through the repo but couldn't find the correct place for such a feature to be implemented.

@mateusfccp
Copy link
Contributor

@aabounegm I can't really say how much would it cost to the team and if it's simple or not, but it's not as simple as simply hacking the SDK.

It all starts with language design, which is what this repository is about, and the Dart language team usually spend a little more time than you would expect in this, because they don't need only to specify it in the language specification, but also consider all the implications this change would bring to the language, specially considering the future of the language evolution.

Once specified, then the many different tools will have to implement it (CFE, analyzer, builder, VM etc) and it is also non-trivial and require efforts for many people from different teams.

That said, maybe someone of the language team could point you better toward the proper way to doing this. Often people will only suggest the language feature, but I am sure that you can help more directly given you have the proper know-how and follow some guidelines...

@aabounegm
Copy link
Author

I understand, and I wanted to contribute, but I have no experience with the internals of Dart, unfortunately. I do know some C++ and am familiar with compilers a little bit, and I care a lot about following guidelines, so I was hoping I could be of help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

2 participants