-
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
Are we thinking about an alias for if null and if not null? #2332
Comments
Personally, I don't like this change, since it makes it less clear when null-checking are happening compared to the amount of characters actually being saved. There are also several cases with existing syntax where I think this could add confusion like e.g. ternary expressions: Also, |
Yeah, my original post was to find an alternative to if null and if not null. The example just to foment the idea. Edited the post and title. Currently checking for null is too verbose to something that happens a lot |
I don't think this is valuable at all... It's not like we make null checks all the time, nor like it's a verbose expression, so... |
I agree, I think "verbose" isn't the right way to describe it. A null check says exactly what it's doing, using syntax we're already familiar with, with no extra words: if (value == null) Read: "If value is/equals null". With a shorter syntax, we'd have to "unwrap" it in our heads as we read it. |
// this
foo = foo == null ? bar : foo;
// became this
foo ??= bar; I am assuming that the reasoning for the shorthand is because the prior is verbose to type and read. IMO I find For example, if we had something like:
We would be validating the nullability independently of the |
There is the option of making null falsy and others truthy |
This example is verbose because it says the word if (foo == null) foo = bar;
...why? Again, it's not verbose, it's literally the minimum amount of characters/information needed to express "check if
Again, how does this make it more "valid" or "correct"? An object's "nullability" just means whether it is or can be equal to |
using As if the So adding a shorthand to nullability also opens room for other possibilities. |
Given how heavily overloaded |
I'd prefer to add some extensions to the language core: extension NullableVar on Object? {
bool get isNull => this == null;
bool get isNotNull => this != null;
}
extension NullableString on String? {
bool get isNullOrEmpty => this == null || this.isEmpty;
bool get isNotNullOrEmpty => this != null && this.isNotEmpty;
}
extension NullableIterable on Iterable? {
bool get isNullOrEmpty => this == null || this.isEmpty;
bool get isNotNullOrEmpty => this != null && this.isNotEmpty;
}
extension NullableMap on Map? {
bool get isNullOrEmpty => this == null || this.isEmpty;
bool get isNotNullOrEmpty => this != null && this.isNotEmpty;
} I'm not sure it would solve the "shorthand" part but it would feel more Darty to have these getters IMO. EditRelated (or dupe) of #190 |
Sorry if already posted, I just couldn't find anything related.
What about a shorthand to
if (foo == null)
andif (foo != null)
?Maybe some simple stuff like
if (foo?) // same as foo == null
if (foo!) // same as foo != null
The text was updated successfully, but these errors were encountered: