scala Unit 和 Nothing 和有什么不一样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13539822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between Unit and Nothing?
提问by Reactormonk
Both types Unitand Nothingindicate a function that does not return anything. What's the difference between them?
这两种类型Unit,并Nothing表示不返回任何一个功能。它们之间有什么区别?
回答by Petr Pudlák
Unitis a type that has exactly one value ? see Unit type. On the other hand, Nothinghas no possiblevalue - see Bottom type.
Unit是一种只有一个值的类型吗?见单位类型。在另一方面,Nothing有没有可能值-见底部类型。
A function that doesn't return anything must have the return type Unit. If it were Nothingthen the function could not return a result. The only way to exit the function would be by an exception.
不返回任何内容的函数必须具有返回类型Unit。如果是,Nothing则该函数无法返回结果。退出函数的唯一方法是通过异常。
Nothingis used in a different way. It is characterized by two properties:
Nothing以不同的方式使用。它有两个特点:
Nothingis a subtype of every other type (includingNull).- There exist no instances of this type.
Nothing是所有其他类型(包括Null)的子类型。- 不存在这种类型的实例。
When is this useful? Consider None:
这什么时候有用?考虑None:
object None extends Option[Nothing]
Because Optionis covariant in its type parameter and Nothingis a subtype of everything, Option[Nothing]is a subtype of Option[A]for every type A. So, we can make one object Nonewhich is a subtype of Option[A]for every A. This is reasonable, since Nothingcannot be instantiated so Option[Nothing]will always be without a value. Similarly
因为Option它的类型参数是协变的,并且Nothing是所有事物Option[Nothing]的子类型,所以Option[A]对于每种类型都是 的子类型A。因此,我们可以创建一个对象None,它是Option[A]for each 的子类型A。这是合理的,因为Nothing无法实例化,因此Option[Nothing]将始终没有值。相似地
object Nil extends List[Nothing]
Unitcorresponds to logical true and Nothingcorresponds to logical false under the Curry-Howard isomorphism, where we view types as propositions and functions as proofs, .
Unit对应于逻辑真,Nothing对应于Curry-Howard 同构下的逻辑假,我们将类型视为命题,将函数视为证明, 。
回答by scriptin
Unitmeans that (a) function has side effects like input and output, (b) these side effects are the main goal of the function. Of course, function can have side effects even if it's type is different from Unit.
Unit意味着(a)函数有输入和输出等副作用,(b)这些副作用是函数的主要目标。当然,即使函数的类型与Unit.
Nothingis a special type in Scala, because (a) it has no values (Unit has exactly one value - ()), so you cannot return value of type Nothingand (b) it is a subtype of every other types. That means, that if something has type Nothing, it can be used in every place where other type is required, but it won't produce any result. This is used for dealing with exceptions - throwexpression has a type of Nothing, so it can be used basically everywhere in a program.
Nothing是 Scala 中的一种特殊类型,因为 (a) 它没有值(Unit 只有一个值 - ()),因此您不能返回类型的值,Nothing并且 (b) 它是所有其他类型的子类型。这意味着,如果某物具有 type Nothing,则可以在需要其他类型的每个地方使用它,但不会产生任何结果。这用于处理异常——throw表达式有一个类型Nothing,所以它基本上可以在程序中的任何地方使用。
Simply, Nothingmeans that there was an error and nothing was returned, while Unitmeans there were side effects.
简单地说,Nothing意味着出现错误并且没有返回任何内容,而Unit意味着有副作用。
Programming in Scalahas a nice explanation of that.
回答by bluenote10
To add one aspect to Petr's reply: Nothingplays an important role in the type hierarchy. It is a bottom type. That means that it is a subtype of every other type, which is like the opposite of Any, which is a supertype of everything. You can find a nice explanation here.
在 Petr 的回复中添加一个方面:Nothing在类型层次结构中扮演重要角色。是底型。这意味着它是所有其他类型的子类型,就像 的相反Any,它是一切的超类型。你可以在这里找到一个很好的解释。
回答by Abhishek Kumar
Unit
单元
Unit is same as voidof Java. void in java is a keyword but Unit is an Object in Kotlin.
单位与Java 的void相同。java中的void是一个关键字,而Kotlin中的Unit是一个对象。
Now if a function returns Unit in Kotlin, it means that it doesn't return anything meaningful, that function just executes a bunch of code and gives back the execution flow.
现在,如果一个函数在 Kotlin 中返回 Unit,则意味着它没有返回任何有意义的东西,该函数只是执行一堆代码并返回执行流程。
Nothing
没有什么
Nothing is a different kind altogether. You can't relate it with anything in Java. There is nothing as such as Nothing in java.
没有什么是完全不同的。你不能把它与 Java 中的任何东西联系起来。在 java 中没有像 Nothing 这样的东西。
Nothing means the end of execution. If a function returns Nothing, means literally nothing it returns. Not even the execution flow.
没有什么意味着执行的结束。如果函数返回 Nothing,则意味着它实际上不返回任何内容。甚至不是执行流程。
Nothing is like a black hole, anything that goes in doesn't come out, Not even the light (light is "execution flow" here). In the case of Unit, least light comes out.
没有什么像黑洞,任何进去的东西都出不来,就连光也不行(这里的光是“执行流”)。在 Unit 的情况下,发出的光最少。
So, if a function returns Nothing, the flow of code ends there and anything written after that is Unreachable.
因此,如果函数返回 Nothing,则代码流到此结束,之后写入的任何内容都是Unreachable。
Now see the code below,
现在看下面的代码,
fun main() {
unitFun()
println("after unit")
nothingFun()
println("after nothing") //you get warning here saying "unreachable code"
}
fun nothingFun(): Nothing {
println("inside nothing")
throw Exception()
}
fun unitFun(): Unit {
println("inside unit")
}
Output will be
输出将是
inside unit
after unit
inside nothing
Exception in thread "main" java.lang.Exception
NOTE:you get warning as soon as you write
注意:你一写就收到警告
println("after nothing") UnReachable Code
And you know the reason, it's because nothingFun doesn't even send back the execution flow.
你知道原因,这是因为 nothingFun 甚至不发回执行流程。
One more thing to complete this here is, any function that returns Nothing has to throw an exception of any kind.
这里要完成的另一件事是,任何返回 Nothing 的函数都必须抛出任何类型的异常。
REASON:If you write some general statement (and not throw an exception) then the function will match the return type and we don't have anything that returns Nothing, means we don't have anything that sucks the execution flow and never returns it back. Everything returns something, even Unit is something.
原因:如果您编写一些通用语句(并且不抛出异常),则该函数将匹配返回类型,并且我们没有任何返回 Nothing 的内容,这意味着我们没有任何阻碍执行流程的内容并且从不返回它背部。一切都会返回一些东西,甚至 Unit 也是一些东西。
Now, one can say that Nothing is a class so we can make object of it and return that itself. Nohhhhhhh? A big nohhhh?
现在,可以说 Nothing 是一个类,因此我们可以创建它的对象并返回它本身。呵呵呵?一个大不?
Nothing class has one constructor but it is private, so you can't even make an object of it.
没有任何类有一个构造函数,但它是私有的,因此您甚至无法创建它的对象。
Last Bit
最后一点
Behind the scene, Unit is converted into void(small v) in the decompiled byte code while Nothing is converted into Void(capital V).
在后台,Unit在反编译的字节码中被转换为void(small v),而Nothing被转换为Void(capital V)。
see the below code,
看下面的代码,
var user = null //First
var user : User? =null //Second
Here, the Second statement shows, userof Nullable-User type. What is the type of userof the first statement?
这里,第二个语句显示,用户为 Nullable-User 类型。第一条语句的用户类型是什么?
any guess?
有什么猜想吗?
It's Nullable-Nothingtype.
它是Nullable-Nothing类型。
try to see the first statement as
尝试将第一条语句视为
var user : Nothing? = null
Now what's happening under the hood,
现在幕后发生的事情,
Void user = (Void)null; //First
User user = (User)null; //Second

