-
Notifications
You must be signed in to change notification settings - Fork 205
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
operators to replace "== null" and "!= null" #190
Comments
I'm a little afraid of having too many meanings of the same operator. Also, it's not clear to me which one is (Maybe it will be less important to do this checking if we had non-nullable types). |
Personally, I don't think these operators are very useful outside of if conditions, |
Please don't do that, I catch all kinds of bugs via our current "it must be a boolean" rule. |
True, but non-nullable types will likely catch most of those statically. |
@munificent I use a lot of nullable types (that is, types that I want "null" to be valid for). |
|
What would be absolutely amazing is a hybrid of ?: and ?? |
We probably can't use All in all, probably not a viable syntax. |
I don't mind the exact syntax - the functionality is what matters |
Introduce at least something for Map, so we can simplify JSON parsing from |
The current plan is to allow |
What if I want to check a function variable is not null, then call it? Normally I would write:
But I would like to write like:
This is very useful when using optional callbacks in Flutter. |
We have discussed supporting foo.bar?.(); I'm not sure if that made it into the NNBD proposal or not. If not, it's a reasonable change we could add later. |
This didn't make it into the initial proposal. I think it's likely something we'll follow up with at some point. |
Couldn't wait for it =) |
For the call-if-not-null case you can use an existing mechanism, the class A {
void Function() bar() => null;
}
main() {
var foo = A();
// Rather than this: ..
if (foo.bar != null) foo.bar();
// .. you could use this:
foo.bar?.call();
} This works because invocation of a function |
Good to know! Thank you! |
Interesting. |
where |
I've also found this to be quite a common pattern. Here's a potential solution. Would be nice to have as part of a main library somewhere. extension CallOnNullAsNull on Function {
// callIfNotNull
R? calln<T, R>(T? arg) => switch (arg) { T value => this(value), null => null };
}
extension IsNotNullThen on Object? {
R? isThen<T, R>(R Function(T value) fn) => switch (this as T?) { T value => fn(value), null => null };
R? nullThen<R>(R Function() fn) => (this == null) ? fn() : null;
}
//e.g.
bool test(int value) => (value == 10);
bool? eg = [0, 1].elementAtOrNull(2).isThen(test);
|
Both forms can be expressed without "if": nullableObject == null ? callFoo() : (); // read () as "DO NOTHING"
nullableObject != null ? callFoo() : (); It's only for those who (like me) dislike single-statement ( |
The main problem with In JS it's very short: Could the following mean if == null? if? (birthday) {
birthday = getBirthday();
check(birthday);
} and the following mean if != null? [ if! (message) Text(message) ] Considering |
To reach the parity with JS, it suffices to introduce a "truthiness" operator like |
You can write that operator today, you just don't get any promotion. extension on Object? {
bool operator ~() => this != null;
} The philosophical objection to making a language feature for it would be that it's not worth it to avoid writing Allowing you to actually abstract over promotion would be more powerful, and would allow people to write more features like this of they want to. |
Oh, I've just realized that the problem lies in the second operand of |
If that's the alternative, then I wouldn't try to avoid the Just write if (variable != null) use(variable); It's idiomatic and easy to read. Converting it to a conditional expression statement with a dummy branch is basically obfuscation. Or use: extension on Object {
T eval<T>(T expression) => expression;
} as variable?.eval(use(variable)) which does nothing if Can call it something else, like |
Indeed, it's idiomatic (because you declared it so, which some style guides disagree with), and arguably readable, but to me, it feels rather ugly . It's not always easy to say why exactly you do not like something, and maybe the following are not the real reasons, but:
if (cond) callX(); // do you write it like this? Or how?
else callY(); Do you think writing it as As for the dummy branch: you can internalize it very quickly, and (Can dart provide some constant of type Never? Then it could be used instead of |
No. The That is also why you should never use cond ? callX() : callY(); as a statement. It sends the wrong signal, that this is an expression with a value that matters, when in fact it doesn't. if (cond) {
callX();
} else {
callY();
} For that, because that looks like what it is: one of two calls which don't return a value. Using You don't want an expression of type Don't use any existing to do a statement's work, it makes the reader think that you intended to have a value. A shorter syntax for |
The difference between if (cond) expr; and if (cond) expr1;
else expr2; is that the latter contains 2 doses of the former. Now it's just a matter of personal sensitivity: for me, even one dose is one too many. 😄 I agree that expressions are always called for "result", but it depends on your definition of "result". In this case, the "result" is the completion of the execution of the expression. Which, by the way, is not always guaranteed in dart, hence the debate about unawaited futures. The |
Dart already provides the beloved and very useful NULL-aware operators ??, ??= and ?.
I'd really like to see operators to replace the tedious "== null" and "!= null" comparisons.
Something along the lines of:
The text was updated successfully, but these errors were encountered: