Monday, 18 December 2017

Introduction to Scala - V

We continue with more Scala on the topic of Mixins in this post. For all the work in this post, we will use Read-Evaluate-Print-Loop Scala interpreter. Some familiarity with Java will be of great help in understanding Scala. In this post, we will look at more of traits in the form of Mixins.

We have already seen Traits in this post. Mixins are traits that can be used to compose a class. The first example is very similar to the one we have seen in the earlier post:

class Class{
  def Printx(Value: Long): Unit = {
    println("Value in Class is " + Value)
  }
}

trait trait6 { def method6 }

trait trait7 extends trait6 { def method6 { println("method7 in trait7") } }

val NewClass = new Class with trait7 {
  override def Printx(Value: Long): Unit = {
  method6
  println("Value in NewClass is " + Value)
  super.Printx(Value)
  }
}


NewClass.Printx(5)

The results are shown below:

scala> class Class{
     |   def Printx(Value: Long): Unit = {
     |     println("Value in Class is " + Value)
     |   }
     | }
defined class Class

scala> trait trait6 { def method6 }
defined trait trait6

scala> trait trait7 extends trait6 { def method6 { println("method7 in trait7") } }
defined trait trait7

scala> val NewClass = new Class with trait7 {
     |   override def Printx(Value: Long): Unit = {
     |   method6
     |   println("Value in NewClass is " + Value)
     |   super.Printx(Value)
     |   }
     | }
NewClass: Class with trait7 = $anon$1@68702e03

scala> NewClass.Printx(5)
method7 in trait7
Value in NewClass is 5
Value in Class is 5


In the above example, we define a class called Class that contains a method called Printx. Next, trait6 is defined having abstract method called method6. trait7 inherits trait6 and implements method6. We then instantiate Class with Mixin trait, trait7 with keyword, with. Note that Printx is overridden with a new definition. If Printx is not overridden, NewClass would have inherited Printx as implemented in trait7. Note also that while the signature of Printx remains the same as seen from outside, internals are modified. Lastly, we call Printx and the results are in line with the definition of Printx with Mixin.

This concludes the topic Mixins.

Sunday, 17 December 2017

Introduction to Scala - IV

We continue with more Scala on the topic of Traits in this post. For all the work in this post, we will use Read-Evaluate-Print-Loop Scala interpreter. Some familiarity with Java will be of great help in understanding Scala. In this post, we will look at the way traits are declared and how traits are called in programs.

Traits can be seen as abstract classes in Scala. Like in Java, a concrete class that extends a trait must implement all methods in the trait definition. Like in Java, traits cannot be instantiated. An example can be seen below:

trait trait1 { def method1 }
trait trait2 { def method2 }
trait trait3 { def method3 }

class TraitImplementation extends trait1 with trait2 with trait3{
def method1 { println ("In method1") }
def method2 { println ("In method2") }
def method3 { println ("In method3") }
}

val TraitImplementation1 = new TraitImplementation
TraitImplementation1.method1
TraitImplementation1.method2
TraitImplementation1.method3


The results are shown below:

scala> trait trait1 { def method1 }
defined trait trait1

scala> trait trait2 { def method2 }
defined trait trait2

scala> trait trait3 { def method3 }
defined trait trait3

scala> class TraitImplementation extends trait1 with trait2 with trait3{
     | def method1 { println ("In method1") }
     | def method2 { println ("In method2") }
     | def method3 { println ("In method3") }
     | }
defined class TraitImplementation

scala> val TraitImplementation1 = new TraitImplementation
TraitImplementation1: TraitImplementation = TraitImplementation@6ee88e21

scala> TraitImplementation1.method1
In method1

scala> TraitImplementation1.method2
In method2

scala> TraitImplementation1.method3
In method3


Three traits are defined having a method definition each. The concrete class called TraitImplementation extends trait1 with keyword extends. But, for inheriting the next traits, we use the with keyword. The three methods are implemented in the class. After that, the class is instantiated, the three methods can be called.

If we try to instantiate a trait, we get below error:

scala> new trait1
<console>:13: error: trait trait1 is abstract; cannot be instantiated
       new trait1
       ^


In case the member names are the same, then, the override keyword has to be used in the class extending the trait as shown below:

trait trait4 { def method4 { println("method4 in trait4") }}

class TraitImplementation extends trait4 {
override def method4 { println("method4 in TraitImplementation") }
}

val TraitImplementation1 = new TraitImplementation
TraitImplementation1.method4


The results are shown below:

scala> trait trait4 { def method4 { println("method4 in trait4") }}
defined trait trait4

scala> class TraitImplementation extends trait4 {
     | override def method4 { println("method4 in TraitImplementation") }
     | }
defined class TraitImplementation

scala> val TraitImplementation1 = new TraitImplementation
TraitImplementation1: TraitImplementation = TraitImplementation@2a136383

scala> TraitImplementation1.method4
method4 in TraitImplementation


As expected, the method overridden in the class is called. If, however, we need the method in the trait to be called, we have to use the super keyword as shown below:

trait trait5 { def method5 { println("method5 in trait5") }}

class TraitImplementation extends trait5 {
override def method5 { super[trait5].method5 }
}

val TraitImplementation1 = new TraitImplementation
TraitImplementation1.method5


The results are shown below:

scala> trait trait5 { def method5 { println("method5 in trait5") }}
defined trait trait5

scala> class TraitImplementation extends trait5 {
     | override def method5 { super[trait5].method5 }
     | }
defined class TraitImplementation

scala> val TraitImplementation1 = new TraitImplementation
TraitImplementation1: TraitImplementation = TraitImplementation@8f39224

scala> TraitImplementation1.method5
method5 in trait5


Now, the method in the parent trait is called.

This concludes the discussion on Traits in Scala.

Introduction to Scala - III

We continue with more Scala on the topic of classes in this post. For all the work in this post, we will use Read-Evaluate-Print-Loop Scala interpreter. Some familiarity with Java will be of great help in understanding Scala. Classes in Scala are similar to classes in any other language. In this post, we will look at the way classes are declared and instantiated in programs.

A class can be defined as shown below:

class FirstClass

val FirstClassInstance1 = new  FirstClass

The output is shown below:

scala> class FirstClass
defined class FirstClass

scala> val FirstClassInstance1 = new  FirstClass
FirstClassInstance1: FirstClass = FirstClass@dca2615


class FirstClass declares a class of FirstClass.

val FirstClassInstance1 = new  FirstClass with the new keyword creates an instance of class, FirstClass and assigns the newly created instance to val, FirstClassInstance1. Like in Java, default constructor of FirstClass is invoked as there are no arguments. dca2615 is the address of the instance in the memory of computer.

We see a constructor in the next example:

class SecondClass(val x:Double,val y:Boolean){
println("x is " + x + " and y is "+ y)
}

val SecondClassInstance1 = new SecondClass(1.0,true)


The results are shown below:

scala> class SecondClass(val x:Double,val y:Boolean){
     | println("x is " + x + " and y is "+ y)
     | }
defined class SecondClass

scala> val SecondClassInstance1 = new SecondClass(1.0,true)
x is 1.0 and y is true
SecondClassInstance1: SecondClass = SecondClass@3b55dd15


We can set default values on the parameters. In the below example, we have set a default value for parameter, x only.

class ThirdClass(val x:Double = 10.0,val y:Boolean){
println("x is " + x + " and y is "+ y)
}

val ThirdClassInstance1 = new ThirdClass(y = true)


The results are shown below:

 scala> class ThirdClass(val x:Double = 10.0,val y:Boolean){
     | println("x is " + x + " and y is "+ y)
     | }
defined class ThirdClass

scala> val ThirdClassInstance1 = new ThirdClass(y = true)
x is 10.0 and y is true
ThirdClassInstance1: ThirdClass = ThirdClass@4dd90166


In the next example, we add a private instance variable and a couple of methods to get the value and set the value of the private instance variable.

class FourthClass {
  private var x:Long = 0
  def Setx(newValue: Long): Unit = {
    x = newValue
  }
  def Getx = x
}

val FourthClassInstance1 = new FourthClass
FourthClassInstance1.Getx
FourthClassInstance1.Setx(100L)
FourthClassInstance1.Getx


The results are shown below:

scala> class FourthClass {
     |   private var x:Long = 0
     |   def Setx(newValue: Long): Unit = {
     |     x = newValue
     |   }
     |   def Getx = x
     | }
defined class FourthClass

scala> val FourthClassInstance1 = new FourthClass
FourthClassInstance1: FourthClass = FourthClass@72543547

scala> FourthClassInstance1.Getx
res9: Long = 0

scala> FourthClassInstance1.Setx(100L)

scala> FourthClassInstance1.Getx
res11: Long = 100


If we try to access the private variable x directly, we get below error:

scala> FourthClassInstance1.x
<console>:14: error: variable x in class FourthClass cannot be accessed in FourthClass
       FourthClassInstance1.x
                            ^


With this we conclude the discussion on Classes in Scala.