Usages of Null / Nothing / Unit in Scala

后端 未结 8 1706
刺人心
刺人心 2020-11-30 18:34

I\'ve just read: http://oldfashionedsoftware.com/2008/08/20/a-post-about-nothing/

As far as I understand, Null is a trait and its only instance is

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 19:13

    I've never actually used the Null type, but you use Unit, where you would on java use void. Nothing is a special type, because as Nathan already mentioned, there can be no instance of Nothing. Nothing is a so called bottom-type, which means, that it is a sub-type of any other type. This (and the contravariant type parameter) is why you can prepend any value to Nil - which is a List[Nothing] - and the list will then be of this elements type. None also if of type Option[Nothing]. Every attempt to access the values inside such a container will throw an exception, because that it the only valid way to return from a method of type Nothing.

提交回复
热议问题