
Understanding null safety in Dart - timsneath
https://dart.dev/null-safety/understanding-null-safety
======
yagodragon
I believe Dart is a more exciting language than Go. It's the most well
designed scripting language out there, that can also be AOT compiled into
native binaries. It's a better JS and a better Java at the same time. It would
be also a killer language for backends and simple websites. Just imagine a
better node.js built around Dart's excellent tooling.

What made Go more successful than Dart was a mix of decisions, luck, and
momentum. On an alternate reality, Dart would swap places with Go. Dartium VM
would have replaced Javascript on the Web and everyone would have been happy.
Go would be seen as an obscure language with missing features that doesn't
really do anything better than java/C#/C++

~~~
earthboundkid
Dart is a better ES5, but a worse ES6. If you compare Dart to the JavaScript
that existed when it was created, it’s great. If you compare it to modern JS,
it’s crap. Modern JS is really, really good. The main problem with it is that
the ecosystem is only just so-so. People still complaining about JS in 2020
are mostly just showing their own ignorance of what the language is now.

~~~
wolco
The lack of a standard lib puts pressure on the ecosystem to provide simple
functionality like leftpad for every project. Managing those is diffcult.

~~~
earthboundkid
Left pad specifically is part of the standard library now:
[https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Refe...](https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart)

Yes, there are still shortcomings, but to me, the biggest shortcoming is too
much legacy crap that doesn’t use the standard library. For example, Express
predates the standard request, response, and URL classes, so it invents its
own. Plus Node doesn’t even fetch FFS. If everyone actually wrote things in
modern JS, the situation would be better.

------
jitl
Does Dart2 offer any compelling features as a language that aren’t also
present in Kotlin, Typescript, Swift, or Haxe? I know many users are
interested in Flutter - but I’m curious more about the language itself.
Whenever I see Flutter examples I’m disappointed I can’t use it with Kotlin
(which already has many specialized backends) or some other language with more
widespread support outside the Google ecosystem.

~~~
satvikpendem
It compiles to JS, ARM, and x86, for web, mobile and desktop support
respectively.

Also, Dart has code generation so you can write your own language features,
like the freezed package which implements algebraic data types even though
Dart doesn't have them natively.

~~~
markdog12
Yeah, I'm not a huge fan of the current code gen methods. They're pretty
hackish and ugly. I understand it's prob the only option right now. Need
partial classes:
[https://github.com/kevmoo/language/blob/5a1e560bb0871f07f682...](https://github.com/kevmoo/language/blob/5a1e560bb0871f07f68206c8e39b01802290de65/working/0678%20-%20partial%20classes/feature-
brainstorm.md)

------
hardwaregeek
Null safety is absolutely essential for me. It's 2020, why are we still having
NullPointerExceptions? However I'm curious about the next frontier of safety.
We've slowly added a lot of nice features around safe use of values and safe
memory. I wonder what's next.

Personally I've been thinking about safe use of arrays. Arrays are such an
important data structure and yet they're horribly unsafe. Every time I do
index math I get that same nervousness that I used to get accessing fields in
nullable data structures. I'd love for a way to verify array indices that
doesn't involve too much dependent typing.

~~~
johnfn
I think the next frontier is things like Rust. I've griped about Rust in the
past but I do think that the borrow checker often feels like I'm coding in the
future - it understands my code on a much deeper level than any other
statically typed language I've used.

I definitely agree with your feelings around array indices. I feel the same
nervousness every time I do a slice or count backwards from the end of an
array. I'm not really sure what the fix would be here, until I can just tell
the computer what I want it to do, though...

~~~
staticassertion
I think the fix is dependent types, right? If the compiler can reason about
the index and the size of the vector, that's one way.

For example;

let x = 5; let y = vec![0; y * 2];

for i in 0..x * 3 { y[x] }

That shouldn't compile.

~~~
anchpop
Dependent types are cool, but I think the best fix would be _refinement
types_. They are sort of similar, except refinement types use a SAT solver to
prove correctness, while dependent types require to prove _everything_
yourself (to the point where realistically something like what you posted
probably wouldn't compile in most dependently typed languages). Refinement
types also compose better.

Because of the SAT solver you have some restrictions on what you can prove,
but they're so much more ergonomic I think it's more than worth it.

~~~
staticassertion
Sure, agreed that refinements are likely the better approach here.

------
Abishek_Muthian
Not a Dart programmer, but looking at it for flutter. I see from Jetbrains
Developer Survey[1] that Dart has grown to 9% from 6% last year[2] which I
think is because of flutter, which has grown its share to 39% in cross-
platform development.

But doesn't it seem reasonable to use TypeScript for flutter now? If you are
not a Googler, Where do you use Dart (outside flutter) in production for a
purpose where it is better than TypeScript?

[1][https://www.jetbrains.com/lp/devecosystem-2020/](https://www.jetbrains.com/lp/devecosystem-2020/)

[2][https://www.jetbrains.com/lp/devecosystem-2019/](https://www.jetbrains.com/lp/devecosystem-2019/)

~~~
syspec
I say this as a huge IntelliJ fan, ask anyone who knows me. My friends joke
that I must be collecting commission checks, for as much as I nudge people
toward IntelliJs awesome IDEs.

Take this survey with a grain of salt, because the results come from IntelliJ
users, and IntelliJs most important and popular product is IntelliJ IDEA, a
Java IDE (and other things)

So the results will skew in that direction for that userbase.

~~~
Abishek_Muthian
I understand what you are suggesting, considering IntelliJ products are
tightly integrated with Android ecosystem there are chances that the survey
results are skewed towards flutter development.

Then again, none of the responses here are outside flutter development!

------
mindvirus
I'm very happy about this - lack of null safety and implicit downcasts are two
of the biggest problems with Dart/Flutter, which I otherwise really enjoy
working in. I've gotten in the habit of adding "assert(x != null)" in all of
my methods.

~~~
cbolton
> I've gotten in the habit of adding "assert(x != null)" in all of my methods

Is this not useless most of the time, since you'll usually get an exception
(with stack trace) when the value is used?

~~~
mindvirus
Yeah, but at least this catches it early - i.e. if a branch/if-statement uses
the field, it might not always be hit when testing things out. Unit tests
_should_ catch these things, but so should the type system, and for my side
projects I'm kinda fast and loose with testing for better or for worse.

------
skocznymroczny
Dart is one of my favourite languages right now. I know it gets a lot of
criticism for not reinventing the wheel and being heavily OOP based, but it's
simple enough, standard library works well, IDEs work great. Also it actually
has some unique features which I now miss from other OOP languages, such as
named constructors - new Angle.fromRadians(...), new Angle.fromDegrees(...) -
feels much cleaner and more self-documenting than the static factory method
other languages have you use.

Optional typing I never really cared about, because even in languages with var
I find myself writing the types whenever possible.

------
chubot
I used flow typing in MyPy and it's awesome. TypeScript has the same thing
(although I would be curious if their rules are significantly different).

I think you can't practically have null safety without flow typing, at least
in an imperative language, otherwise you end up with casts everywhere. MyPy
has null safety, though it takes some effort to turn it on for most codebases.

~~~
millimeterman
Not at all - that kind of dataflow analysis is basically just a hack to patch
over the fundamentally broken design of having everything be nullable. It's
totally unnecessary in a language with proper algebraic data types where
there's no all-pervasive concept of "nullability". The only imperative
language I know of that has this is Rust, but it originates from ML languages
like OCaml and Haskell.

It's one of those features where once you get used to it, it's hard to fathom
how anyone programs without it. It's such a simple yet expressive feature
that's incredibly useful when writing normal, ordinary code. Nothing fancy or
theoretical.

------
ElliotH
I'm really hoping this will make AngularDart a lot nicer to write. Inputs
might have a null value and it's very easy to forget to account for it, will
be great to catch that at compile time.

------
olodus
Looks to be very close to the solution Zig also went for. I like the solution
and it seems to fit both languages very well. I should look a bit further into
Dart some time I think.

