Generics Contravariance in Scala <font size=-2><a href=http://www.lifemichael.com/en/?page_id=73 target=_blank>PRO</a></font>

When using generics Scala doesn’t support covariance. The following code doesn’t compile. object HelloSample { def main(args:Array[String]):Unit = { val a:MyStack[SportCar] = new MyStack[SportCar]; val b:MyStack[Car] = a; } } class Car class SportCar extends Car class MyStack[T] The following video clip shows the compilation error we get while trying to compile this code and […]

Functions as Objects in Scala <font size=-2><a href=http://www.lifemichael.com/en/?page_id=73 target=_blank>PRO</a></font>

Function values are treated as objects. The function A=>B is an abbreviation for using a new object instantiated from a class that extends the scala.Function1[A,B] trait and overrides the apply function.┬áThere are currently Function1, Function2, Function3… etc… up to Function22, that takes 22 parameters. The following code sample shows that. object HelloSample { def main(args:Array[String]):Unit […]

The Nothing Type in Scala <font size=-2><a href=http://www.lifemichael.com/en/?page_id=73 target=_blank>PRO</a></font>

The main usage for Nothing is when specifying the return type for methods which never return normally (e.g. throw an exception when been called). object Sample { def main(args:Array[String]):Unit = { val ob = getMyStack[Int](10) } def getMyStack[T](num:Int) = { new EmptyMyStack[T] } } abstract class MyStack[T](size:Int) { def data:T; } class EmptyMyStack[T] extends MyStack[T](0) […]

Generic Functions in Scala <font size=-2><a href=http://www.lifemichael.com/en/?page_id=73 target=_blank>PRO</a></font>

The Scala programming language allows us to define generic functions. This code sample shows that. object Sample { def main(args:Array[String]):Unit = { val ob = getMyStack[Int](10) } def getMyStack[T](num:Int) = { new MyStack[T](num) } } class MyStack[T](size:Int) { //… } The following video clips overviews this code sample, shows its execution and provides more information.