Showing posts with label aacaj| Nothing. Show all posts
Showing posts with label aacaj| Nothing. Show all posts

Friday, September 23, 2016

When to use 'Nothing'...

scala> //One of the usage of Nothing is with

scala> //abnormal program termination #253

scala>

scala> ///////////////////////////////////

scala> //  An Example WITHOUT Nothing

scala> ///////////////////////////////////

scala> def error(msg:String): Unit = {
     |   println("Message.. is -> " + msg)
     |   throw new RuntimeException(msg)
     | }
error: (msg: String)Unit

scala>

scala> def isValidNo(no: Int):Boolean = {
     |   if(no == 0) {
     |     error("Invalid no")
     |   }else {
     |     true
     |   }
     | }
<console>:45: error: type mismatch;
 found   : Unit
 required: Boolean
           error("Invalid no")
                ^

scala>

scala> ///////////////////////////////////

scala> //  An Example WITH Nothing

scala> ///////////////////////////////////

scala> def error(msg:String): Nothing = {
     |   println("Message.. is -> " + msg)
     |   throw new RuntimeException(msg)
     | }
error: (msg: String)Nothing

scala>

scala> def isValidNo(no: Int):Boolean = {
     |   if(no == 0) {
     |     error("Invalid no")
     |   }else {
     |     true
     |   }
     | }
isValidNo: (no: Int)Boolean

Scala Class Hierarchy...

  1. Any #247
    1. Available Methods #246
      1. ==
      2. !=
      3. equals
      4. ##
      5. hashCode
      6. toString
    2. AnyVal (Value Class)
      1. All primitive types falls under this category
      2. It is also possible to create our own AnyVal types #254
      3. ----------------
      4. Double
      5. Float
      6. Long
      7. Int
        1. RichInt(Booster Class) ; Contains Implicit conversion methods...#249
      8. Short
      9. Byte
      10. Char 
      11. Boolean
      12. Unit
        1. Is represented by () #247
        2. Is equivalent to Void
      13. ...
      14. ----------------
    3. AnyRef (Reference Classes) (Is the equivalent of Object in Java) #250
      1. String
      2. Iterable
      3. Seq
      4. List
      5. ...
      6. ----------------
      7. Useful Methods
      8. ----------------
      9. ==  #252
      10. eq
  2. Bottom Classes
    1. Null
      1. Is a subclass of every other Reference Classes
      2. Used for null reference #252
    2. Nothing
      1. Is a subclass of every other class
      2. Can be used to signal abnormal termination etc... #253