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

Add an operator !?? for when an expression is not null #4007

Closed
feinstein opened this issue Aug 5, 2024 · 2 comments
Closed

Add an operator !?? for when an expression is not null #4007

feinstein opened this issue Aug 5, 2024 · 2 comments
Labels
request Requests to resolve a particular developer problem state-duplicate This issue or pull request already exists

Comments

@feinstein
Copy link

I apologize in advance if this issue already exists, I didn't find it.

Currently we have ?? for assigning values when the left operand is null, but I keep finding myself on the case where I need the opposite, to only do something when it's not null. Example:

// Currently I have to do this:

final x = y != null ? getValue(y) : null;

// Or not use final variables and lose compile-time checks.

// I wish I could do this:
final x = y !?? getValue(y); // x will be null if y is null, otherwise we assign the result of getValue(y) to x

I recognize this is syntactic sugar, low priority, and adds a new operator in the language, but I find this pattern happening so recurrently, I wish it could be simpler.

@feinstein feinstein added the request Requests to resolve a particular developer problem label Aug 5, 2024
@airai-ad-117
Copy link

the concept is good ,but syntax is very confusing ,
in my private lib. ,i have :

extension nil__conversion__extension<type extends Object> on type? {
  result__type? convert__if<result__type>(
    final result__type Function(type) operate,
  ) {
    final this_1 = this;

    if (this_1 == null) {
      return NIL;
    }

    return operate(
      this_1,
    );
  }
}

@lrhn
Copy link
Member

lrhn commented Aug 5, 2024

The most common operation on something that is not null is to call a method on it, and we have that: x?.method().
This does promote x in the selector chain.

So, what you can do is:

extension IfNotNull on Object {
  R notNull<R>(R value) => value;
}

void main() {
  // 21, but compiler doesn't know.
  int? x = DateTime.now().millisecondsSinceEpoch > 0 ? 21 : null;
  var y = x?.notNull(x * 2); // x promoted after `?.`.
  print(y == 42); // true
}

So it's possible, you just have to declare a helper extension method and use ?.

There are numerous requests for something like this. As @mateusfccp says #1592 is likely the most recent open one, and a number of issues linked from that. It's not a slam-dunk for a number of reasons.

  • It's not always well-defined what the operator should do. It's a variable promotion operator, since it doesn't actually use the value of the expression it applies to, which means that it only really works with variables. Should it be restricted to variables on the left? Or do you want to do: somethingBig() !?? somethingRelated() without actually promoting any variable.
  • It's not always easy to scope (depending on what is being asked for).
  • Having null as the value in the other case isn't always the right thing.

And writing x == null ? null : expression isn't that bad, so there is no real urgency to find a solution. It's not a high priority, as you say, which means that other things have generally taken precedence.

Going to close this and defer to #1592.

@lrhn lrhn closed this as completed Aug 5, 2024
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Aug 5, 2024
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 state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants