Why to use Scala???

Martin Odersky released the first version of Scala in 2004.

Hybrid:
Scala unites two worlds: object-oriented programming and functional programming.

Integration with Java:
Seamlessly integrates with Java and it’s ecosystem. The Scala compiler compiles the Scala source code to byte code which runs on JVM.

It’s syntax is familiar, yet much more concise than Java. You can visit Scala vs Java for a fair comparison.

Type inference:

Reducing boilerplate makes code easier to read and write. Omitting the type signature variables and functions makes the code easier to scan. In more complicated blocks of code, it is useful to specify the type anyway to help understand the code and to leverage the compiler’s assistance when debugging a compilation error.

Named parameters/default parameters:

This makes it very simple to set up the default arguments to a method, and selectively override them from the invocation.

Traits:

This is effectively multiple inheritance, done right. Traits allow for common functionality to be implemented separately, and then mixed in to a class. This is like the ability to add implementing code to an interface.

Tuples:

Unlike an array or list, a tuple can hold objects with different types .These are also immutable.

Here’s a tuple that contains an Int and a String:
val tuple = (50, “Gyan”)

Objects instead of static methods:

In Scala, static doesn’t exist. You cannot add static methods to classes. Instead, you can define objects. When you define an object with the same name as a class, then that object is the companion object of that class.

Methods that you define in the companion object of a class are like static methods of a class in Java.

Lazy evaluation:

strict evaluation: compute the value of an expression immediately.

lazy evaluation:is an evaluation strategy which delays the evaluation of an expression until its value is needed.

Scala is strict by default, but lazy if explicitly specified for given variables or parameters.

lazy val : evaluates the first time that it’s accessed and stores the result.

Pattern Matching:

Pattern matching is a mechanism for checking a value against a pattern.Scala provides great support for pattern matching.

implicit:

Scala provides support for the below four types of implicit.
1. implicit parameters
2. implicit functions
3. implicit classes
4. implicit objects

higher order functions:

In Scala , you can create higher order functions which are functions that take other functions as parameters.

Nested functions:

Scala allows you to define functions inside a function and functions defined inside other functions are called local functions.

The following object provides a factorial method for computing the factorial of a given number:

 def factorial(x: Int): Int = {
    def fact(x: Int, accumulator: Int): Int = {
      if (x <= 1) accumulator
      else fact(x - 1, x * accumulator)
    }  
    fact(x, 1)
 }

 println("Factorial of 2: " + factorial(2))
 println("Factorial of 3: " + factorial(3))

The output of this program is:

Factorial of 2: 2
Factorial of 3: 6

Collections:

Scala has a rich set of collection library. Collections are containers of things. Those containers can be sequenced, linear sets of items like List, Tuple, Option, Map, etc. The collections may have an arbitrary number of elements or be bounded to zero or one element (e.g., Option).

Collections may be strict or lazy. Lazy collections have elements that may not consume memory until they are accessed, like Ranges. Additionally, collections may be mutable (the contents of the reference can change) or immutable (the thing that a reference refers to is never changed). Note that immutable collections may contain mutable items.

Immutable by default:

Immutability is key to functional programming.

“Immutable” means that you can’t change (mutate) your variables; you mark them as final in Java, or use the val keyword in Scala.

In Java , we have to add the final keyword to enforce immutability.

In Scala, the opposite approach is taken; with functions, function arguments are val by default.

Scala idiom:

Prefer val over var. Only use var when you have to.

Scala takes this much further, with a collections hierarchy that splits the collections into “immutable” and “mutable” data structures.

If you create a new Map or Set, you automatically get the immutable structures.

From the excellent book, Programming in Scala, by Odersky, Spoon, and Venners, there is a small section titled, “A balanced attitude for Scala programmers”:

“Prefer vals, immutable objects, and methods without side effects. Reach for them first. Use vars, mutable objects, and methods with side effects when you have a specific need and justification for them.”

Leave a Reply

Your email address will not be published. Required fields are marked *