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

4

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?

    
asked by anonymous 24.10.2017 / 19:30

1 answer

4

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

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

So you can make a indexOf() on an object that is a string , but can not call this method on a Stream

Do not confuse with What's the difference between a static and dynamic programming language? .

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

Again in Kotlin all done at compile time, even though they are not a requirement to set it like this.

This is interesting to avoid the so-called billion-dollar mistake than the Tony Hoare is self-critical for having invented the possibility that objects may have a null value.

Having a null value somehow violates type security, then a language is completely type safety if it is also null safety .

You can not var texto : String = null; .

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

Documentation .

Of course the subject is a bit more complex than this.

When writing an article, it is good to completely master the subject so that you do not risk writing something wrong. It is very easy to think that something is one way and actually be another. The internet is full of articles with the wrong content.

    
24.10.2017 / 19:42