Unit
類似于Java中的void
。Unit
是類型,而它的實例是()
scala> def nothing():Unit={}
nothing: ()Unit
scala> nothing()==()
res6: Boolean = true
scala> nothing()==Unit
res8: Boolean = false
想測試nothing
返回值是不是Unit
,不要把它和Unit
作比較,因為這是在用值和類型比較,就好像5==Int
一樣沒有意義。而應該和()
比較,也就是值和值的比較。