根据Scala规范,
scala.Nothing 类型 - 所有类型的按钮。
类型“Nothing”存在,但 Nothing 的实例不存在。
这个怎么运作:
def ??? : Nothing = throw new NoImplementedError
def sys.error(message: String): Nothing = throw new RuntimeException()
def sys.exit(status: Int): Nothing = {...}
但实际上,所有提到的方法都返回异常。异常(exception)
def sys.exit
你能否澄清更多关于Nothing类型的信息。任何例子,解释。谢谢!
最佳答案
def ??? : Nothing = throw new NoImplementedError
不返回异常,它抛出一个不同的异常。异常是一种控制流机制,它使控制立即跳转到调用堆栈中最近安装的处理程序。这意味着在
val x = ???
x
永远不会被赋值,所以 x
可以有任何类型。这种类型是Nothing
在 scala 类型系统中,它是所有类型的子类型。非终止也没有类型,因为它也从不返回值,例如
def loop(): Nothing = loop()
val x: Int = loop()
因此是允许的,因为
x
永远不会被分配。
关于scala - 理解scala.Nothing类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35848904/