Type Safety and Null safety in Dart.
Tags: dynamic, dart, var, soundsafety
Published: 2023-06-03 12:40:50

## Have you ever wondered why Dart is called a Type Safe language?

It is so becauase the only operations that can be performed on data in the language are those allowed by the type of the data.

In short the operations that are defined for `String` can not be applied on `int`.

for example -
`int x;
 x.toUpperCase();` is not allowed in dart.

**Let's take a deep dive into this.**

There must be some tools in the Dart programming language which is analyzing whether the developer is writing typesafe code or not? Well Yes! There are two such type of tools..

(i) Static Type Check - Checks at Compile time.
(ii) Runtime check - Checks at runtime.

This whole system is referred as Sound Type System.

**But Why is it called so?**

Whenever we type code on our keyboard we make noise right? imagine that for everykey we press the sound print is being sent to the "Dart Static Analyzer" it checks everything and makes sure that the code we wrote is type safe when it detects that something isn't right it will display an error and it won't let us run the program until the issue is resolved.

We can think of Static type check as a local type check which happens whenever a change is made.

While the runtime check is like an additional Check which happens at the runtime.

But Sometimes the developer might know what they are doing they would want to stop the Static checking of the code.

**In that case we use the `dynamic` data type.**

dynamic ignores the Static type check. We can declare a variable of dynamic type and the Static type check will not care about it anymore but the runtime check will care about it and will show an exception if a wrong method is applied to it.

for example -
`dynamic b = 3.14
b.toUpperCase();` in this case the program will run because the static type check will not show any error but the at the runtime the runtime check will throw an exception that the methodn `toUpperCase()` is not defined for a value of double.

## Type Interference and Dynamic vs Var in Dart
Types are mandatory in dart but they don't need to be annotated because Dart can Infer types by using the `var` keyword.

example - 
`var x = 5.4;` 
In this case the static type check will will think that variable x was assigned to a double value then it's type must be double for it's entire lifetime.

but in somecases we wouldn't want that therefore we can use `dynamic x = 5.4` and we can put any other type  of value in x now like -
`x = 'abcd';
x = 41;
x = anyFunction();` 

Runtime check will dynamically infer the type of x at the runtime while Static type check will not even care about x.

**Here comes the null safety of Dart**

What if we do not initialize a var at the time of declaration?

ex- `var a;`

Dart analyzer will set the the type of `a` as dynamic throughout it's lifetime, Here dart analyzer played it safe and passed the job to the runtime check. But the problem is that the runtime check also doesn't know about the type of `a` because it is uninitialized. 

hence the runtime check will set it's value to null.

This is not a good practice..
**Therefore Dart introduced a new feature Sound Null Safety.**

It means that the value can't be null unless the developer say's that it is null.

which means Data can either be nullable or nun-nullable but can't be both during the runtime.

This is the reason why you might see Null type exception at the runtime.

If a dart variable is non-nullable that variable will always be non-nullable also non-nullability is retained at the runtime.