In Kotlin - Unit and Nothing

Unit and Nothing in Kotlin

Unit

package kotlin

/**
 * The type with only one value: the `Unit` object. This type corresponds to the `void` type in Java.
 */
public object Unit {
    
    
    override fun toString() = "kotlin.Unit"
}

Unit in Kotlin is equivalent to void in Java in type, but there are some differences.

  • void in Java is a keyword. Unit itself is a objectsingleton represented by , so it can be understood that Kotlin has a class
  • In Kotlin, every method/function is an expression. Expressions always have a value, so every method must have a return value. If it is not returnspecified explicitly, then generally it will be added automatically for usUnit
fun method() :Unit{
    
    

}
fun method(){
    
    

}

The above two methods are the same. :Unitcan be omitted.

Nothing

package kotlin

/**
 * Nothing has no instances. You can use Nothing to represent "a value that never exists": for example,
 * if a function has the return type of Nothing, it means that it never returns (always throws an exception).
 */
public class Nothing private constructor()

In Kotlin, all normal methods have a return value, at least one returns Unit, but there is also a method with a return type of Nothing. This method is not normal, or rather it throws an exception.

Everything has examples. You can use Nothing to mean "a value that never exists": eg: If a function has a return type of Nothing, it means it will never return (always throws an exception).

When we define a function, the given return type is Nothing:

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-Zapfdfoz-1687659149507) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230625095835948.png)]

It can be seen that an error is reported when compiling directly. For the compiler, you clearly told him that the return value does not exist, but you can also return a value, and he directly emo...

At this time we should throw an exception. You can modify it like this:

fun main() {
    
    
   method("抛出异常")
}

fun method(error:String) :Nothing{
    
    
    throw Exception(error)
}

Results of the:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-FYVwVVF4-1687659149509) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230625100217926.png)]

It is similar in Kotlin TODO: TODOthe only function of the function is抛出异常

fun test3(){
    
    
    TODO()
}

@kotlin.internal.InlineOnly
public inline fun TODO(): Nothing = throw NotImplementedError()

This TODO function can throw an exception under any function. However, if you are careful enough, you will find that test3()the return value type of the method should be the default Unit, or other self-defined return type, but in fact, because it is called, TODO()it returns one Nothing(and does not return normally), so the compilation can pass. Why? Woolen cloth?

Some articles Nothingunderstand it as a subclass of all other classes, that is to say, Nothingis a subclass of other classes, so where you return other classes, you can use a return Nothinginstead , and this understanding is no problem. But there is a question, isn't Kotlin&Java single inheritance? If Nothingit is a subclass of all classes, isn't that multiple inheritance? In fact, there is a concept here. In fact, "inheritance" and "subclassing" are two concepts. We say that class A inherits class B, that is, A extends B, which emphasizes some implementations in B, which can be reused in A; and class A is a class The subclass of B emphasizes that class A can be used where class B is needed. The angles of the two are different. Single inheritance means that from the perspective of inheritance, the top-level parent class of all classes is Any(in Java Object), then everyone can reuse the existing implementation of the parent class. And subclassing can be replaced in all places that need to use the parent class. For example, if I need to return a method Any, then I actually return an Intobject, which is no problem. Therefore, Nothingthe return type that can replace the original method is from the perspective of subclassing. Because it can be replaced, it Nothingis a subclass of all classes, which does not conflict with single inheritance.

Guess you like

Origin blog.csdn.net/qq_43867812/article/details/131371948