Generics Wild Card in Scala PRO
When defining a variable of a generic type we can use a wild card instead of specifying the exact secondary type the generic type uses. 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 […]
Generics Contravariance in Scala PRO
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 […]
Type Bounds in Scala PRO
When we define a function or a class and we choose to use generics we can set limits on the unknown type: S <: T – means that S is a sub type of T S >: T – means that S is a super type of T It is also possible to mix between […]
Functions and Anonymous Classes in Scala PRO
When calling a function we indirectly invoke the apply method on the object that represents the function. We can use the anonymous inner class syntax for defining a new anonymous inner class that extends the Function relevant trait in order to create a new function. The following code sample shows that. object HelloSample { def […]
Functions as Objects in Scala PRO
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 […]