Scala – Introduction

Origin

Scala was created by Martin Odersky in 2004. The name ‘Scala’ is derived from the word ‘Scalable’ which signifies that it can grow with the demand of the user. Scala was designed to be both an Object oriented as well as a Functional programming language. i.e. every value is an object and every function is a value. It is designed to exhibit common programming patterns in a concise, type-safe and elegant way. Many say that it was introduced to fulfill the shortcomings of Java.

1_ygv4NHtC9Jao-aZPSCMPYg

Why Scala?

  • Modular, highly efficient and scalable.
  • Is a type-safe JVM language that incorporates both object-oriented and functional programming.
  • Good for exploiting parallelism for multicore and parallel computing.
  • The source code compiles to Java bytecode that runs on the JVM.
  • It is very flexible in defining abstractions
  • Immutability: Scala makes it easy to write code using immutable data
  • Scala can use all Java libraries and Scala code can be imported into Java code interchangeably.
  •  Applications:
    • Writing web applications
    • Distributed applications
    • Streaming Data
    • Parallel processing
    • Analyzing data with Spark

Who’s using Scala?

businesses-using-scala.jpg

Directory structure

SBT or the Source Build Tool is an open-source build tool for Scala/Java Projects. Here’s the directory structure normally followed in Scala projects. The src/ is the Base or the project’s root directory.

src/
   main/
       resources/
         <files to include in main jar here>
       scala/
         <main Scala sources>
       java/
         <main Java sources>
   test/
       resources
         <files to include in test jar here>
       scala/
         <test Scala sources>
       java/
         <test Java sources>

Class, Object, Package and Trait

Class: The concept of classes in Scala is similar to that in Java. They are nothing but templates containing fields and methods. A class can be instantiated using the ‘new’ construct. Classes in Scala cannot have static members.

Object: Object is a named instance with fields and methods.

Package: Packages allows you to modularize programs. Contents of the package can be scattered across many files. Classes defined in a package can be specifically imported.

Trait: Traits are like Interfaces. However, they can even contain field definition or method implementations.

Writing your first Scala Program:

object FirstScalaProg extends App {
    println("I am done! Execute me!")
}

The entry point of the program is defined inside an object. The object is made executable by extending the type ‘App‘ or by adding a “main” method, as follows:

object FirstScalaProg {
    def main(args: Array[String]){
        println("I can be written this way too!")
    }
}

Scala source code is stored in text files with the extension .scala. The Scala compiler compiles .scala files into .class files. Classfiles are binary files containing machine code for the JVM to execute.

Books:

  • Structure and Interpretation of Computer ProgramsHarold Abelson and Gerald J. Sussman. 2nd edition. MIT Press 1996.– [Full text available online].
  • Programming in ScalaMartin Odersky, Lex Spoon and Bill Venners. 2nd edition. Artima 2010. – [Full text of 1st edition available online]
  • Scala for the ImpatientCay Horstmann. Addison-Wesley 2012. – [First part available for download.]
  • Scala in DepthJoshua D. Suereth. Manning 2012.
  • Programming ScalaDean Wampler and Alex Payne. O’Reilly 2009.

Other Learning Sources:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s