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

"if not null" operators #131

Open
har79 opened this issue Dec 12, 2018 · 4 comments
Open

"if not null" operators #131

har79 opened this issue Dec 12, 2018 · 4 comments
Labels
request Requests to resolve a particular developer problem

Comments

@har79
Copy link

har79 commented Dec 12, 2018

Dart supports "if null" access and assignment operators — ??, ??=.

Equivalent "if not null" operators would also be useful, particularly when non-nullable types land (#110). They would allow concise use of nullable variables in a similar manner to quiver's Optional. E.g., instead of:

Optional<Foo> foo;
foo = foo.transform(changeFoo);
final bar = foo.transform(fooToBar);

nullable variables could be used:

Foo? foo;
foo ?!= changeFoo(foo);
final bar = foo ?! fooToBar(foo);
@lrhn lrhn added the request Requests to resolve a particular developer problem label Dec 12, 2018
@lrhn
Copy link
Member

lrhn commented Dec 12, 2018

It is a common pattern to "do something if something is not null". The ?. operation allows calling methods on an object if it is not null, but there is no similar feature to guard a general operation by something being not-null (the opposite of ??).

The ?! operator is probably not viable (the sequence can occur naturally, e.g., x ?! b : c).

@har79
Copy link
Author

har79 commented Dec 12, 2018

Good point, !? and !?= should be a more viable alternative and more memorable since it matches the structure of the long-form equivalent: x ! /*= null*/ ? b /*: null*/

@DonizeteVida
Copy link

Something like that:

border: borderColor ?? Border.all(color: borderColor, width: 1),

border is optional, so I have to do:

border: borderColor != null
? Border.all(color: borderColor, width: 1)
: null,

border: borderColor !? Border.all(color: borderColor, width: 1),

@llucax
Copy link

llucax commented Sep 24, 2020

Probably not very compatible with Dart explicit bool rules, but another option would be to allow an exception with null and use the && short-circuit operator like in C-like languages: border: borderColor && Border.all(color: boderColor, width: 1).

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