gpt4 book ai didi

scala - 理解scala.Nothing类型

转载 作者:行者123 更新时间:2023-12-03 20:18:40 24 4
gpt4 key购买 nike

根据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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com