Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 11: Scala's Hierarchy > 11.4 Conclusion - Pg. 216

C HAPTER 11 · Scala's Hierarchy scala> val i: Int = null <console>:4: error: type mismatch; found : Null(null) required: Int Type Nothing is at the very bottom of Scala's class hierarchy; it is a sub- type of every other type. However, there exist no values of this type whatso- ever. Why does it make sense to have a type without values? As discussed in Section 7.4, one use of Nothing is that it signals abnormal termination. For instance there's the error method in the Predef object of Scala's standard library, which is defined like this: def error(message: String): Nothing = throw new RuntimeException(message) The return type of error is Nothing , which tells users that the method will not return normally (it throws an exception instead). Because Nothing is a subtype of every other type, you can use methods like error in very flexible ways. For instance: