Usages of Null / Nothing / Unit in Scala

后端 未结 8 1715
刺人心
刺人心 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:11

    Here's an example of Nothing from scala.predef:

      def ??? : Nothing = throw new NotImplementedError
    

    In case you're unfamiliar (and search engines can't search on it) ??? is Scala's placeholder function for anything that hasn't been implemented yet. Just like Kotlin's TODO.

    You can use the same trick when creating mock objects: override unused methods with a custom notUsed method. The advantage of not using ??? is that you won't get compile warnings for things you never intend to implement.

提交回复
热议问题