Google Dart gains sound null safety
Google’s Dart language is being fitted with sound null safety, helping developers avoid a class of bugs that can be hard to spot. “Sound” null safety means that Dart is 100% sure that non-nullable variables cannot be null.
Now in preview, Dart’s null safety also improves performance, members of the Dart team said on June 10. The new feature is largest addition to the language since the introduction of Dart 2, launched in August 2018. Null safety is available for tryout in a version of DartPad, and due in a production-ready Dart release by the end of 2020.
Dart is a type-safe language, meaning the compiler can guarantee the type of a variable. But type safety itself does not guarantee that a variable is not null. Null errors are common, having led to many issues in Dart code and many commits trying to fix those issues. Null safety lets developers reason about code with more confidence, without having to worry about runtime null dereferencing errors. Developers instead receive static errors as they code.
Soundness in null safety enables Dart programs to be faster and smaller. Dart analyzes code, determines that a variable is non-nullable, and ensures tat it is always non-nullable. The ahead-of-time compiler can produce smaller and faster code because it does not need to add checks for nulls when it knows a variable is not null.
The Dart team is endeavoring to make null safety as easy to use as possible. Also, null safety is backward compatible and not a breaking change. It will be an optional feature, allowing developers to adopt it whenever they’re ready. Dart core libraries have been migrated to use null safety.
Design principles of Dart’s sound null safety include:
- Non-nullable by default. Developers must explicitly note that a variable can be null, or it will be considered non-nullable. Non-null is common in APIs.
- Incrementally adoptable, with tools to be provided to help with migration.
- Fully sound, with projects reaping the benefits of soundness once migrated.
This story, “Google Dart gains sound null safety” was originally published by