I understand null safety in dart. It means every variable should not be null by default.
But what is the difference between null safety and sound null safety?
CodePudding user response:
null safety
A Dart program can contain some libraries that are null safe and some that aren’t. These mixed-version programs execute with unsound null safety.
sound null safety
When a program is fully migrated and all its libraries are null safe, then it runs with sound null safety, with all of the guarantees and compiler optimizations that soundness enables.
CodePudding user response:
Simplified, "sound" null safety guarantees that, unless you use some "hacks" like ! operator, you will never get a null exception.
You can read more about it here: https://dart.dev/null-safety/understanding-null-safety
Specifically, this part:
The resulting null safe code should be fully sound. “Soundness” in the context of static checking means different things to different people. For us, in the context of null safety, that means that if an expression has a static type that does not permit null, then no possible execution of that expression can ever evaluate to null. The language provides this guarantee mostly through static checks, but there can be some runtime checks involved too. (Though, note the first principle: any place where those runtime checks happen will be your choice.)
Soundness is important for user confidence. A boat that mostly stays afloat is not one you’re enthused to brave the open seas on. But it’s also important for our intrepid compiler hackers. When the language makes hard guarantees about semantic properties of a program, it means that the compiler can perform optimizations that assume those properties are true. When it comes to null, it means we can generate smaller code that eliminates unneeded null checks, and faster code that doesn’t need to verify a receiver is non-null before calling methods on it.
One caveat: We only guarantee soundness in Dart programs that are fully null safe. Dart supports programs that contain a mixture of newer null safe code and older legacy code. In these mixed-version programs, null reference errors may still occur. In a mixed-version program, you get all of the static safety benefits in the portions that are null safe, but you don’t get full runtime soundness until the entire application is null safe.
CodePudding user response:
Sound null safety makes types in code non-nullable by default and enables special static checks and compiler optimizations to guarantee that null-dereference errors won't appear at runtime because they will be spotted at compile-time and fixed.
Null safety is a guarantee within an object-oriented programming language that no object references will have null or void values. ... In such cases, the call above will be a void call, leading to a run-time exception, often resulting in abnormal termination of the program.
CodePudding user response:
Sound mean valid. null safety is a broad term, and sound null safety means valid (usage, or implementation of) null safety.
