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

Scala class, traits, and OO programming


As a hybrid post-functional language, Scala allows you to write OO code and create classes as well. Right now we will learn how to create classes and functions inside classes, and also how to work with traits, which are similar to Java interfaces in concept but way more powerful in practice.

A simple Scala class in Scala REPL

We will see a simple Scala class in Scala REPL as follows:

$ 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 Calculator {
     |    def add(a: Int, b: Int): Int = a + b
     |    def multiply(n: Int, f: Int): Int = n * f
     | }
defined class Calculator
scala> 
scala> val c = new Calculator
c: Calculator = Calculator@380fb434
scala> c.add(1,2)
res0: Int = 3
scala> c.multiply(3,2)
res1: Int = 6
scala>

At first glance, the preceding code looks like Java. But let's add constructors, getters, and setters, and then you can see how much we can accomplish with just a few lines of code.

Scala plain old Java object in Scala REPL

Following is a Scala plain old Java object 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> val p  = new Person("Diego",31)
p: Person = name: DIEGO age: 31
scala> val p1 = new Person(age = 31, name = "Diego")
p1: Person = name: DIEGO age: 31
scala> p.getAge
res0: Int = 31
scala> p1.getName
res1: String = DIEGO
scala> 

Constructors in Scala are just lines of code. You might realize that we get the name variable, and apply a function to change the given name to upper case in the preceding example. If you want, you can put as many lines as you want, and you can perform as many computations as you wish.

On this same code, we perform method overriding as well, because we override the toString method. In Scala, in order to do an override, you need to use the override operator in front of the function definition.

We just wrote a Plain Old Java Object (POJO) with very few lines of code in Scala. Scala has a special annotation called @scala.beans.BeanProperty, which generates the getter and setter method for you. This is very useful, and saves lots of lines of code. However, the target needs to be public; you can't a apply BeanProperty annotation on top of a private var or val object.

Person class in Java

Following is a Person class in Java:

    package scalabook.javacode.chap1; 
 
    public class JavaPerson { 
   
      private String name; 
      private Integer age; 
   
      public JavaPerson() {} 
 
      public JavaPerson(String name, Integer age) { 
        super(); 
        this.name = name; 
        this.age = age; 
      } 
 
      public JavaPerson(String name) { 
        super(); 
        this.name = name; 
      } 
 
      public JavaPerson(Integer age) { 
        super(); 
        this.age = age; 
      } 
     
      public Integer getAge() { 
        return age; 
      } 
 
      public void setAge(Integer age) { 
        this.age = age; 
      } 
 
      public String getName() { 
        return name; 
      } 
 
      public void setName(String name) { 
        this.name = name; 
      } 
   
    }