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
Easier way to detect if object is null #8598
Comments
This comment was originally written by bryonmarks...@gmail.com Perhaps slightly different, but very much related, I'd love to have Null-Conditional Operators (http://msdn.microsoft.com/en-us/magazine/dn802602.aspx) |
This comment was originally written by @Emasoft Yes, +1 for null-conditional operators and null-propagation in Dart. That would solve many problems and would make the code much more concise. |
Removed Area-Library label. |
I am sympathetic to the idea of Object.isNull. This is really a library issue: adding get isNull => false to Object, and a version that returns true to Null. Of course, adding methods to Object at this stage is risky, since someone out in the universe may have defined isNull already. So I doubt if we'll do this before 2.0. Set owner to @gbracha. |
I don't think this is a good idea. I agree that it's a matter of aesthetics, which makes it completely subjective, but I subjectively disagree on the aesthetics being better. Removed Area-LanguageFeature label. |
@lrhn Apologies for gravedigging but how It's weird having to write It's looks strange in a mostly-words-dot-notations-and-maps-code (Flutter) and sometimes it's visually harder to read as it breaks the flow of it. |
that can be easily done with extension methods: https://dartpad.dartlang.org/ed0a241f8e6cb9d8b5d77f889f7800b1 extension AdvObj on Object {
bool get isNotNull => this != null;
} |
There are two significant differences between The Second, the In some cases, the check for |
In a dynamic scripting language, typing this feels weird:
if (obj != null)
I'd much rather do this:
if (obj)
But because I don't expect that to happen, might I suggest the following:
if (obj.isNull)
This is similar to the helpers we have like isEven and isEmpty.
Here's how I think isNull can be added:
Object.isNull returns false, unless the instance is of the class Null, in which case it returns true. Much like runtimeType is final, I think 'final get isNull' can be added. I'd go as far as to say it is a compile-time warning if isNull is implemented by a class.
I admit this is an aesthetic thing, but aesthetics matter.
Slight bonus, this drives home the point that null is an object.
The text was updated successfully, but these errors were encountered: