-
Notifications
You must be signed in to change notification settings - Fork 227
Description
The approach taken by Dart to address null-safety is currently a NNBD model. I am currious to know why it as been prefered to what I would call a NNBO (non-nullable by option) model and if this has even been considered?
I think that it is always a bad thing for a langage to make some assumptions on behalf of the developper. Here, with NNBD, it assume that any variable is non-nullable unless explicitly declared as so (with the ? type) by the developer. It would have make more sense to do the reverse and let all variables be nullable unless declared as not with the bang operator (!) after the type declaration. That way, devloppers that want to track, before runtime, unexpected null values that may occur in their code would still be able to do it but it would also allow to use any third party libs that did not yet migrate to null-safety smoothly (an error would simply occur at runtime if some unexpected null value appear in the code of this lib until the developpers optionaly migrate it to null-safety (which may not be necessary if they already correctly manually tested for null values everywhere in the code, so no null can be unexpected))
So why not allow Dart to use an NNBO approach for developers that want to (which, I think, most would favor compared to NNBD)?
My proposal would be to have Dart to be able to work with an NNBD or an NNBO approach, depending on some compiler flag
So in your Dart code, you could declare a variable of type T in three different ways:
T myvar;
T? myvar; //<- myvar can be of type T or null
T! myvar; //<- myvar is of type T and nether null when compiling with the NNBD option, T myvar would resume to be the same thing as T! myvar (meaning myvar can't be null) whereas with the NNBO option T myvar would be the same as T? myvar
That would allow to write null-safety code that would work with both approches (if the developper knows if a variable is nullable or not, he just have to tag its type with a ? or a !) and would resolve many of the current developer headaches to migrate their code to null-safety with third party libraries that did not migrate yet or part of their code where unexpected null values are already correctly manually addressed (so migrating this code to null-safety is just an unecessary waste of time)