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
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 Class Hierarchy...
- Any #247
- Available Methods #246
- ==
- !=
- equals
- ##
- hashCode
- toString
- AnyVal (Value Class)
- All primitive types falls under this category
- It is also possible to create our own AnyVal types #254
- ----------------
- Double
- Float
- Long
- Int
- RichInt(Booster Class) ; Contains Implicit conversion methods...#249
- Short
- Byte
- Char
- Boolean
- Unit
- Is represented by () #247
- Is equivalent to Void
- ...
- ----------------
- AnyRef (Reference Classes) (Is the equivalent of Object in Java) #250
- String
- Iterable
- Seq
- List
- ...
- ----------------
- Useful Methods
- ----------------
- == #252
- eq
- Bottom Classes
- Null
- Is a subclass of every other Reference Classes
- Used for null reference #252
- Nothing
- Is a subclass of every other class
- Can be used to signal abnormal termination etc... #253
Subscribe to:
Posts (Atom)