Skip to content

Cleaner Null-Check Syntax #66

@DartBot

Description

@DartBot

This issue was originally filed by eiger...@gmail.com


It would behoove Dart to adopt a cleaner syntax for null checking than the existing...

if (exampleVar != null) {
    exampleVar.doStuff();
}

Having PHP-like functionality would require altering the language spec on boolean values to null equating to false. This would allow the following:

if (exampleVar) {
    exampleVar.doStuff();
}

Another option that would not require adjusting the boolean value language spec is adding a groovy-like null-safe operator to the syntax:

exampleVar?.doStuff();

This is handy, but it would not allow making logically different decisions based on null status - something that may or may not be necessary.

Another option would be implementing the ? operator as a null-check in the parent Object class. Then, we could do...

if (exampleVar?) {
    exampleVar.doStuff();
}

There are other options, too - any of them that would reduce the syntax weight would be helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-duplicateClosed in favor of an existing report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions