What is the difference between Type-safe and Null-safe?

Asked

Viewed 1,727 times

6

I’m writing an article about Kotlin, and I came across these guys, if anyone can help me.

What is the difference between Type-safe and Null-safe?

1 answer

6


Type-safe is a feature that an object is always of the expected type and the compiler can determine whether an operation can be done on it. There will be no indefinite behavior if it fails, there may be error or some rule on how to resolve the issue.

In Kotlin the check is done at compile time avoiding problems in the execution and processing expense. But the feature does not require it to be done at this time.

Then you can make one indexOf() in an object that is a string, but can not call this method in a Stream for example.

Not to be confused with What is the difference between a static and dynamic programming language?.

Null-safe (also known as void Safety) is the property that ensures that an object is never null, or if it is null it is required due treatment to not give error.

Again on Kotlin all done at compilation time, even if it is not a requirement to define like this.

This is interesting to avoid the call Billion-dollar mistake that the Tony Hoare criticizes itself for having invented the possibility that objects may have a null value.

Having a null value somehow violates the security of types, so a language is only completely type Safety if she goes too null Safety.

You can’t do it var texto : String = null;.

If you declare the cancellable type var texto : String? = null; then fits the null, but it can only be accessed if check before if the variable is null, something like texto?.length which is an operator that executes only if it is not null.

Documentation.

Of course the subject is a little more complex than that.

When you write an article it is good to completely master the subject so as not to risk writing something wrong. It’s very easy to find that something is one way and actually being another. The internet is full of articles with wrong content.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.