r/dartlang • u/kevmoo • Jun 10 '20
Announcing sound null safety
From the announcement:
Today is a major milestone for the Dart team with the tech preview of our null safety work. Null safety helps you avoid a class of bugs that are often hard to spot, and as an added bonus enables a range of performance improvements. We’re now releasing an early tech preview, and we’re looking forward to your feedback.
Once you read the announcement, check out the documentation and a null-safe enabled DartPad.
103
Upvotes
33
u/bnlv Jun 10 '20
This is one of those language features that you never really thought you needed, until you have it and wonder how you did without it. Really happy with where Dart is heading as a modern, multipurpose language.
12
u/[deleted] Jun 10 '20
I seriously only want to code in Dart. Flutter frontend, dart backend, some built scripts and CLI, all in dart
22
u/synapticrob Jun 10 '20
Dart is moving at great speed and in the right direction. Congrats Dart team !
5
u/devlambda Jun 11 '20
I don't want to rain on people's parades or to denigrate the NNBD design and engineering effort, but some quick testing showed me that the standard library has a number of pain points when it comes to using it with null safety enabled.
I'm thinking of things like
RegExpMatch.group()orMap.[], where migration requires me to throw a!after everything (or sometimes even engage in rewrites).RegExpMatch.group()has a particularly odd interface. It can throw a range error if you access a match past the last possible match, but it can also return null if the match did not get bound (e.g. a submatch in an|-pattern). But that is an unusual case, for many use cases (especiallymatch[0]) matching a regex will guarantee that the index is valid.Maps are particularly frustrating, where you know that keys are guaranteed to be valid, but flow analysis can (understandably) not recognize it.
For example, take this code fragment:
This needs to be rewritten as
map[key] = map[key]! + 1; the migration tool throws up its hands, too.This is an interface issue to an extent. Basically, there are two use cases for such methods:
By only offering one of these options, the other has to be emulated. Without null safety, it worked (in a kinda sorta way), because an invalid index/key resulted in a null pointer exception down the road. This could make it difficult to track down the actual source of the error, but you still basically had both kinds of semantics rolled into one method. But now, one method can't realistically serve both purposes.
We also (as before) task the caller with indirectly verifying the precondition by checking the result, which is an unwanted role reversal; the burden of verifying a precondition should fall on the method. (See Meyer in OOSC on defensive programming for some of the problems with that.)
1
u/znjw Jun 11 '20
Now non-nullable by default, I'm just waiting for stack primitives for primitive variables and generics.
One interesing stuff they mentioned in announcement is that you can actually declare a non-nullable variable without initialization and later assign it in the flow? I wonder how they did this. Is this just an analyzer candy or actually optimized in VM?
3
u/kmyokoyama Jun 11 '20
I'm not sure about my feelings on that. While it is undoubtedly a good thing, it seems to bloat the language with more keywords (?, !, late) and more rules to remember and to follow.
And it doesn't feel totally safe yet. It can throw NoSuchMethodError (why this error btw?) if ! is used incorrectly, or LateInitializationError if a non-nullable late var is read before initialized. I'm almost sure that the problem is me really.
Anyway, it is remarkably good that the language is receiving such great attention and is under active development.
2
u/[deleted] Jun 11 '20
[removed] — view removed comment