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

Evaluate to null if condition is met #1592

Open
rubenferreira97 opened this issue Apr 19, 2021 · 7 comments
Open

Evaluate to null if condition is met #1592

rubenferreira97 opened this issue Apr 19, 2021 · 7 comments
Labels
feature Proposed language feature that solves one or more problems null-aware-expressions Issues proposing new expressions to manage null

Comments

@rubenferreira97
Copy link

rubenferreira97 commented Apr 19, 2021

Given Flutter uses dart as its programming language I will write this proposal here.
Flutter have a lot of optional arguments. Given that, most of the time there is a need to return/evaluate to null.

Some examples are:
Render an optional (custom) widget given a condition.

Center(
    child: renderWidget ? CustomWidget() : null,
)

Return no error message (null) if a condition is not met.

TextFormField(
    validator: (value) => nameController.text.isEmpty 
                          ? AppLocalizations.of(context).formEmptyValidation  
                          : null,
)

etc...

It would be reasonable to have a operator like ?* or something else in Dart?
nameController.text.isEmpty ?* AppLocalizations.of(context).formEmptyValidation

In fairness, some could argue that this seems an unnecessary operator in Dart, however in flutter development I would use this operator more than ??= for example.

@rubenferreira97 rubenferreira97 added the feature Proposed language feature that solves one or more problems label Apr 19, 2021
@Levi-Lesches
Copy link

Levi-Lesches commented May 10, 2021

Personally, I use this pattern a lot too. My shortcut is to negate the condition so I can put null first, like so:

Center(
  child: !data.user.condition() ? null : ReallyBigWidget(
    children: [
      With(),
      Lots(),
      Of(),
      Parameters(),
    ]
  )
)

That way, I don't have to have the : null all the way at the end.


Also, I'd like an operator for only doing something if a value is non-null. This is different than ?. because it can be used in a for loop or passed to another function. For example:

class MyData {
  static List<MyData?> getList(List json) => [
    for (final dynamic nestedJson in json)
      nestedJson == null ? null : 
        MyData.fromJson(Map.from(nestedJson))
  ];
}

Using !! as an example operator, I'd love to rewrite this:

class MyData {
  static List<MyData?> getList(List json) => [
    for (final dynamic nestedJson in json)
      MyData.fromJson(Map.from(nestedJson!!))
  ];
}

Where !! would short-circuit the expression an evaluate to null (EDIT: See @ramsestom's better syntax below)

@ramsestom
Copy link

ramsestom commented May 20, 2021

Using !! as an example operator, I'd love to rewrite this:

class MyData {
  static List<MyData?> getList(List json) => [
    for (final dynamic nestedJson in json)
      MyData.fromJson(Map.from(nestedJson!!))
  ];
}

Where !! would short-circuit the expression an evaluate to null

I agree that an operator that would allow to do someting if a value isn't null would be usefull.
Personaly I would be more in favor of an operator that would counter mimic the ?? operator. For example If this operator was named !! your example would write as:

class MyData {
  static List<MyData?> getList(List json) => [
    for (final dynamic nestedJson in json)
      nestedJson !! MyData.fromJson(Map.from(nestedJson))
  ];
}

@Levi-Lesches
Copy link

Yep, that's definitely a better way of putting it. I'd love to see it happen

@ykmnkmi
Copy link

ykmnkmi commented May 20, 2021

Also

a !!= b;

like

if (b != null) a = b;

@ramsestom
Copy link

Also

a !!= b;

like

if (b != null) a = b;

well a !!= b would be equivalent to

if (a != null) a = b;

but yes, the idea is to have an operator that behave exactly as the oposite of ??
so you could do things like

a !!= tranformFunction(a); //a function that that would transform a if not null

if you want

@Levi-Lesches
Copy link

Levi-Lesches commented May 20, 2021

Regarding the initial proposal, how about modifying condition ? whenTrue : whenFalse to make the : whenFalse part optional? I don't know if this will conflict with other grammar rules, but it would make sense, given that "if the value is not there, set it to null" is pretty intuitive.

// These two are equivalent
final String? name = isSignedIn ? getUsername(Auth.user);
final String? name = isSignedIn ? getUsername(Auth.user) : null;

EDIT: Pretty confident it can be done since currently Dart throws an error saying expected ":", which means it must be pretty unambiguous that the ? is meant for ternary and not null-safety.

@Levi-Lesches
Copy link

I found a list of similar null operators over here, thanks to @lrhn. Specifically,

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 null-aware-expressions Issues proposing new expressions to manage null
Projects
None yet
Development

No branches or pull requests

5 participants