【发布时间】:2018-06-26 12:13:04
【问题描述】:
作为subtype of every other type 允许将假设的Nothing 类型值传递给任何函数。然而,虽然这样的值可以作为toString() 的接收者,但它不能作为unary_!(以及其他)的接收者。
object Foo {
def dead(q: Nothing): Unit = {
println(q);
q.toString();
((b: Boolean) => !b)(q);
!q; // value unary_! is not a member of Nothing
}
}
这是错误还是功能?
注意:
- 这是 equivalent question I asked on Kotlin 的 Scala 版本。
- 上传作品:
!(q.asInstanceOf[Boolean])
【问题讨论】:
-
一般来说,子类型化 != 继承,尽管大多数 OOP 主流语言倾向于混淆它,因此这两个概念看起来几乎是同义词。
-
假设您可以调用任何可能在
Nothing类型的变量上定义的方法,允许这样的程序有什么好处?
标签: scala inheritance types subtyping bottom-type