Tipi non Definiti

Any, AnyRef e AnyVal sono classi, ma questo non ha corispondenza con Java ed il bytecode sulla jvm, in Java abbiamo le "primive" (int,double..) che non si possono estendere e si usano con gli operatori e non con dei metodi.

AnyRef e AnyVal estendono Any, il sotto tipo di tutto e' Nothing che serve a definire None come Option[Nothing] e Nil come List[Nothing]

scala> def getAStringMaybe(num: Int): Option[String] = {
     |   if ( num >= 0 ) Some("A positive number!")
     |   else None // A number less than 0?  Impossible!
     | }



scala> Nil
res4: Nil.type = List()

scala> Nil.length
res5: Int = 0

scala> Nil + "ABC"
res6: List[java.lang.String] = List(ABC)

Da leggere: http://oldfashionedsoftware.com/2008/08/20/a-post-about-nothing/

Nothing si definisce come:

def ??? : Nothing = throw new NotImplementedError

con ??? che sta per "anything that hasn't been implemented yet"

Null e' un tratto e serve a dare compatibilita' al "null" di Java e le lib in uso.

scala> def tryit(thing: Null): Unit = { println("That worked!"); }
tryit: (Null)Unit

scala> tryit("hey")
<console>:6: error: type mismatch;
 found   : java.lang.String("hey")
 required: Null
       tryit("hey")
             ^

scala> val someRef: String = null   //// !!! va in errore sulla compiulazione sara' un riferimento a null a run time !!!!
someRef: String = null

scala> tryit(someRef)
<console>:7: error: type mismatch;
 found   : String
 required: Null
       tryit(someRef)
             ^

scala> tryit(null)
That worked!

scala> val nullRef: Null = null
nullRef: Null = null

scala> tryit(nullRef)
That worked!

Unit e' il "void" di Java

def execute(code: => Unit):Unit = {
  // do something before
  code
  // do something after
}