Unit和()

作者: Azur_wxj | 来源:发表于2018-02-19 21:24 被阅读14次

Unit类似于Java中的voidUnit是类型,而它的实例是()

scala> def nothing():Unit={}
nothing: ()Unit

scala> nothing()==()
res6: Boolean = true

scala> nothing()==Unit
res8: Boolean = false

想测试nothing返回值是不是Unit,不要把它和Unit作比较,因为这是在用值和类型比较,就好像5==Int一样没有意义。而应该和()比较,也就是值和值的比较。

相关文章

网友评论

      本文标题:Unit和()

      本文链接:https://www.haomeiwen.com/subject/ezrwtftx.html