A High Level Overview Of Scala Programming Language

Scala Programming

Scala Programming
Scala is a computer programming language originally developed by Martin Odersky. The language combines object oriented and functional programing approaches for flexibility. The language shares similarities with other programming languages like Java and C# especially in basic operations, using control structures and specifying data types. However Java is the most widely compared language to Scala in the programming languages literature. Scala is implemented as a JVM language so it compiles to bytecode giving similar performance to Java and ability to inter-operate with Java classes and libraries.

In this tutorial we will review functional programming features available in the language. We will also review object oriented features of the language. Finally we will look at how the two approaches are combined. This tutorial assumes you have set up a Scala development environment in Eclipse or your favorite IDE. If you have not please refer to setting up Scala development environment tutorial.

Classes and inheritance are the first obvious features that need to be implemented in an object oriented programming (OOP) language. These two features are fully supported in Scala to enable development of modular software. Other features are traits, objects and case classes. In this tutorial we will limit ourselves to discussing only the features mentioned.

A class is an OOP technique that helps in organizing code. Classes are used as templates for other objects. The template contains information about fields, constructors, methods, inherited classes and other interface implementations. A class is defined using the class keyword using the style shown below.
class [extends ] [{ fields, methods, and inherited classes }]
Fields are variables that can be accessed by the entire object. We will create a class below to demonstrate that. Start Scala in interactive mode by running scala at a terminal.

define cls
A class that can be only instantiated once is referred to as an object in Scala. In OOP this is referred to as a singleton. The keyword object is used to define an object. You dont need to specify any parameters because instantiation is automatic. You just need to define fields, methods and classes. The structure of defining an object is shown below. You provide the object name and code to be implemented in between curly braces. The curly braces are used in Scala to enforce scoping rules.
object [extends ] [{ fields, methods, and classes }]

first obj
Case classes is another feature that is supported in Scala. A case class when instantiated automatically generates its methods. A companion object is also generated that includes methods that are automatically generated. The parameter list of the class is used to form the basis of the methods in companion object and the class.

Case classes are excellent for implementing classes that store data when data methods have already been generated. Hierarchical classes are not a good choice for use with case classes because inherited fields are not used in its methods. There is nothing special about these methods except automatic generation. You can opt not to use them and implement your own. The benefit they bring is saving you the hassle of correctly implementing all methods in data based classes.

The keyword case is used to define a case class using the structure shown below.
case class ([var] : [, … ])
[extends ()]
[{ fields and methods }]

An example is shown below.

case class
Another OOP feature in Scala is traits. A trait is an OOP concept which is a class that allows multiple inheritance. Classes, case classes, objects and traits can only extend one class but they can extend multiple classes. One key difference between classes and other types is the lack of instantiation. Traits are similar to other classes but like objects they do not need class parameters. Traits differ from objects in that you can use type parameters to make them usable.

To create a trait you use the trait keyword instead of the class keyword. Syntax follows the structure shown below.
trait [extends ] [{ fields, methods, and classes }]
An example of creating a trait is shown below.

The second half of this tutorial will discuss functional programming features available in Scala.

Immutability is a key concept in functional programing which is fully supported in Scala. Variables are marked as immutable by use of the val keyword. When created this way they cannot be changed. By default functions and their arguments are immutable so there is no effort required to make them immutable. Let us use an example function to demonstrate that

The variable miles is immutable and any attempt to change it results in an error as shown below

Another feature that supports immutability is that the Predef automatically places immutable data structures like set in your environment path. So whenever you create a data structure like set it will be immutable.

Functions is another major building block in functional programming approach. Pure functions only operate on their parameters and return results without making changes externally.

Lazy evaluation is another aspect that is supported in functional programming. This feature ensures variables defined as lazy are only executed when they are accessed for the very first time unlike normal variables that once defined they are executed. Function parameters that also get delayed execution are called by-name function parameters. A combination of by name parameters and lazy variables is used to implement infinite data structures.

This tutorial introduced you to the Scala programming language. Object oriented and functional programming concepts were discussed. The object oriented concepts discussed were classes, objects, traits and case classes. Functional concepts discussed were immutability, functions and lazy evaluation.


Please enter your comment!
Please enter your name here