简体   繁体   English

了解scala.Nothing类型

[英]Understanding of scala.Nothing type

According to the scala specification, scala.Nothing type - buttom for all types. 根据scala规范,scala.Nothing类型-所有类型的buttom。 Type "Nothing" exists but instance of Nothing does not exists. 类型“ Nothing”存在,但Nothing实例不存在。

How it works: 这个怎么运作:

def ??? : Nothing = throw new NoImplementedError
def sys.error(message: String): Nothing = throw new RuntimeException()
def sys.exit(status: Int): Nothing = {...}

But in fact, all of mentioned methods return exception. 但是实际上,所有提到的方法都返回异常。 Excepted def sys.exit Could you please clarify more about type Nothing. def sys.exit能否请您进一步说明Nothing类型。 Any examples, explanations. 任何例子,解释。

Thanks! 谢谢!

def ??? : Nothing = throw new NoImplementedError

does not return an exception, it throws an exception which is not the same thing. 没有返回异常,而是引发了与众不同的异常。 Exceptions are a control-flow mechanism which causes control to immediately jump to the nearest installed handler within the call stack. 例外情况是一种控制流机制,该机制会导致控制立即跳转到调用堆栈中最近的已安装处理程序。 This means that in 这意味着

val x = ???

x will never be assigned a value, so x can have any type at all. x永远不会被赋值,所以x可以有任何类型。 This type is Nothing in the scala type system which is the subtype of all types. 在scala类型系统中,此类型为Nothing ,它是所有类型的子类型。

Non-termination also has type nothing since it also never returns a value eg 非终止也没有任何类型,因为它也从不返回值,例如

def loop(): Nothing = loop()

val x: Int = loop()

is therefore allowed since x will never be assigned. 由于x永远不会分配,因此允许。

In Scala absolutely everything needs to have a return type. 在Scala中,绝对所有内容都需要具有返回类型。

println for example have the return type Unit because it's a unit of calculations that returns no value. 例如, println具有返回类型Unit因为它是不返回值的计算单位。

If everything needs to have a return type then, how would you implement a function like ??? 如果一切都需要具有返回类型,那么您将如何实现像???这样的函数??? , so that it can be used by any function , no matter what type it needs to return? ,因此它可以由任何函数使用无论它需要返回哪种类型

Well that is what Nothing is for, it's a subtype of every other types (as Any is the top type of every other type), so a function that returns Nothing can be used anywhere and the type system will always stay consistent. 嗯,这就是Nothing目的,它是所有其他类型的子类型(因为Any是所有其他类型的顶级类型),因此可以在任何地方使用返回Nothing的函数,并且类型系统将始终保持一致。

As you already noticed functions returning Nothing never return a value. 正如您已经注意到,返回Nothing函数永远不会返回值。 That is because there is no value instenciable for Nothing . 那是因为没有Nothing是不可理解的价值。
You can see it as a way to make the compiler "happy" for functions that only throws or stop the app or loop forever. 您可以将其视为使编译器“满意”仅永久抛出或停止应用程序或循环的功能的一种方式。


In case you wonder why we don't use Unit instead of Nothing , here is an example: 如果您想知道为什么我们不使用Unit而不是Nothing ,那么这里是一个示例:

def notYetImplemented: Unit = throw new Exception() 
def myFunc: String = notYetImplemented

This will not compile because Unit is not a String or a subtype of String . 这不会编译,因为Unit是不是String或亚型String So you will need to do: 因此,您需要执行以下操作:

def myFunc: String = {
  notYetImplemented
  null
}

Which is not very convenient and for us to write a value to return even taught we will never reach this code. 这不是很方便,即使我们教我们写一个值来返回值,也永远不会到达此代码。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM