DEV Community

Visakh Vijayan
Visakh Vijayan

Posted on • Updated on

What the hell is type-safe language?

Consider you are a compiler. Now suppose you are a person who doesn't care about your fellow programmers who are writing code and using you to get them compiled. Like for example:

Someone is assigning an integer(4 bytes) into a char(1 byte) variable.

If you are not careful, that assignment will take away the adjacent 3 bytes away. Poof!.

This is type-unsafe. That is the compiler or YOU are not taking care of such validations before compiling them.

Again this can be at two levels:

  1. Static typing - one which is done at compilation time.
  2. Dynamic typing - one which is done at run-time. This is when you get all those run-time exceptions.

Be type-Safe!

Top comments (5)

 
s_anastasov profile image
Stojan Anastasov

I never said you would write 0 unit tests. You would write no tests for types.

Kotlin has nullability built into the language, so I don't have to write a test what happens when the variable I am passing is null.

I also don't have to do defensive programming and check if the variable is null.

Collapse
 
s_anastasov profile image
Stojan Anastasov • Edited

Well, you could have the compiler verify the types for you or write unit tests instead..

How else do you make sure the function expecting a User doesn't get a Monkey instead?

Collapse
 
fpuffer profile image
Frank Puffer • Edited

What does dynamic typing have to do with "Divide by zero"? Zero is a value, not a type. Statically typed languages won't prevent division by zero.

Collapse
 
camto profile image
Benjamin Philippe Applegate

True, unless you have refinement typing!

Collapse
 
vjnvisakh profile image
Visakh Vijayan

True ... that was a mistake. Thank you for pointing it out.