Book Image

Building Applications with Scala

By : Diego Pacheco
Book Image

Building Applications with Scala

By: Diego Pacheco

Overview of this book

<p>Scala is known for incorporating both object-oriented and functional programming into a concise and extremely powerful package. However, creating an app in Scala can get a little tricky because of the complexity the language has. This book will help you dive straight into app development by creating a real, reactive, and functional application. We will provide you with practical examples and instructions using a hands-on approach that will give you a firm grounding in reactive functional principles.</p> <p>The book will take you through all the fundamentals of app development within Scala as you build an application piece by piece. We’ve made sure to incorporate everything you need from setting up to building reports and scaling architecture. This book also covers the most useful tools available in the Scala ecosystem, such as Slick, Play, and Akka, and a whole lot more. It will help you unlock the secrets of building your own up-to-date Scala application while maximizing performance and scalability.</p>
Table of Contents (17 chapters)
Building Applications with Scala
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface

Traits and inheritance


It's possible to do inheritance in Scala as well. For such a task, you use the operator extend after the class definition. Scala just allows you to extend one class, just like Java. Java does not allow multiple inheritance like C++. However, Scala allows it by using the Mixing technique with traits. Scala traits are like Java interface, but you can also add concrete code, and you are allowed to have as many traits as you want in your code.

Scala inheritance code in Scala REPL

Following is a Scala inheritance code in Scala REPL:

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77).
Type in expressions for evaluation. Or try :help.
scala> class Person(
     |   @scala.beans.BeanProperty var name:String = "",
     |   @scala.beans.BeanProperty var age:Int = 0
     | ){
     |    name = name.toUpperCase
     |    override def toString = "name: " + name + " age: " + age
     | }
defined class Person
scala> 
scala> class LowerCasePerson(name:String,age:Int) extends Person(name,age) {
     |    setName(name.toLowerCase)
     | }
defined class LowerCasePerson
scala> 
scala> val p  = new LowerCasePerson("DIEGO PACHECO",31)
p: LowerCasePerson = name: diego pacheco age: 31
scala> p.getName
res0: String = diego pacheco
scala> 

Scala does not make constructors inheritance like Java. So you need to rewrite the constructors and pass the values through a super class. All code inside the class will be the secondary constructor. All code inside parentheses () in the class definition will be the primary constructor. It's possible to have multiple constructors using the this operator. For this particular implementation, we changed the default behavior and added new constructor code in order to make the given name lower case, instead of the default uppercase defined by the Person superclass.

Scala traits sample code in Scala REPL

Following is a Scala traits sample code in Scala REPL:

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77).
Type in expressions for evaluation. Or try :help.
scala> trait Car
defined trait Car
scala> 
scala> trait SportCar {
     |    val brand:String 
     |    def run():String = "Rghhhhh Rghhhhh Rghhhhh...."
     | }
defined trait SportCar
scala> 
scala> trait Printable {
     |    def printIt:Unit 
     | }
defined trait Printable
scala> 
scala> class BMW extends Car with SportCar with Printable{
     |    override val brand = "BMW"
     |    override def printIt:Unit = println(brand + " does " + run() )
     | }
defined class BMW
scala> 
scala> val x1 = new BMW
x1: BMW = BMW@22a71081
scala> x1.printIt
BMW does Rghhhhh Rghhhhh Rghhhhh....
scala>

In the preceding code, we created multiple traits. One is called Car, which is the mother trait. Traits support inheritance as well, and we have it with the SportCar trait which extends from the Car trait. The SportCar trait demands a variable called brand, and defines a concrete implementation of the function run. Finally, we have a class called BMW which extends from multiple traits -- this technique is called mixing.

Scala traits using variable mixing technique at Scala REPL

Following is a Scala traits using variable mixing technique at Scala REPL:

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77).
Type in expressions for evaluation. Or try :help.
scala> trait SportCar {
     |    def run():String = "Rghhhhh Rghhhhh Rghhhhh...."
     | }
defined trait SportCar
scala> 
scala> val bmw = new Object with SportCar
bmw: SportCar = $anon$1@ed17bee
scala> bmw.run
res0: String = Rghhhhh Rghhhhh Rghhhhh....
scala>

Scala is a very powerful language indeed. It's possible to add traits to a variable at runtime. When you define a variable, you can use the with operator after the assignment. This is a very useful feature, because it makes it easier to make function composition. You can have multiple specialized traits and just add them in your variables as you need them.

Scala allows you to create the type alias as well, this is a very simple technique which will increase the readability of your code. It's just a simple alias.

Scala type alias sample in Scala REPL

Following is a Scala type alias sample in Scala REPL:

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77).
Type in expressions for evaluation. Or try :help.
scala> type Email = String
defined type alias Email
scala> 
scala> val e = new Email("[email protected]")
e: String = [email protected]
scala>

When you are coding with Scala, it is highly recommended that you use the type alias and traits for everything, because that way you will get more advantages with your compiler, and you will avoid writing unnecessary code and unnecessary unit tests.