Connecting...

Pexels Photo 280264

(a little bit of) Scala in 6 minutes

Pexels Photo 280264

If you're new to the Scala world then it is always good to learn the basics first. Find out a little bit of Scala and how it compares from other languages in this interesting read from Computing Science Student, Grazietta Hof

 

'Scala is a high-level programming language that has both functional and object-oriented language features. It is designed to be concise, and fast. Scala is compiled to Java bytecode. The executable then runs on a Java Virtual Machine (JVM). Because it is compiled to Java bytecode, Scala has seamless interoperability with Java giving it access to useful Java source code libraries.

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way.

 

Programming Paradigms

Scala is a multi-paradigm language that combines both functional and object-oriented features. This allows for programmers who are familiar with object-oriented concepts to make use of object-oriented styles such as classes but also has the benefit of using functional concepts in their programming such as the use of map and filter on collections instead of using loops. This has the advantage of code being more expressive and thus more readable.

 

Object Oriented

Scala is a pure object-oriented in the sense that every value is a type, even functions and numerical values. Since values are class-based, all values are instances of a class. 'Any' is the supertype of all classes and has several universal methods such as 'equals', 'hashcode', 'toString'. 'Any' has two subclasses 'AnyVal' and 'AnyRef'.

'AnyRef' represent reference types. Every user defined types are classified as reference types.

 

Functional

One of the core values of functional programming is that functions should be first-class.

Scala is functional because every function has a value: functions can be passed around as variables (first class functions). What this means is that like standard functions, they can be declared and invoked. But they can also be passed as a parameter to other functions and returned by other functions.

 

Never Assigned an Identifier

In this example, let’s have a look at the function 'map' — used to return a new list with the function applied to each element of the original list. The function 'x => x * 2' passed into map is anonymous and never assigned an identifier.

 

Be Passed as a Parameter

The code below shows the function 'isAlien' passed in as a parameter to 'filter' demonstrating the functional aspects of Scala.

 

Be Returned by Another Function 

Another feature of functional programming is that functions can return functions. The code below shows a useful use case of this. Sometimes we need to test the logic of a certain function. This can become problematic when functions are dependant on other functions, for example a function that queries a database. It then becomes difficult to test the logic of a single function. If a function is dependant on a function that queries a database, and the unit test for that function fails it becomes difficult to tell if the query to the database failed, or the function logic itself. In scala, we can make use of its function paradigm to isolate dependancies from function logic. Below, the function 'fake_dependancy' simulates a database query that is always successful, and returns a string. The 'method_that_takes_dependancy' function takes in a function (the database query) and returns a new function that contains the logic we need to test. Now, since we know the result of the database query is correct, if we test the function we can be sure we are testing the logic only.

 

Type System Features

Scala can be classified as a statically typed language with type inference. Scala is compiled using the Java Virtual Machine (JVM) or the .NET Common Language Runtime (CLR), and type correctness is checked at compile time. Scala is also strongly typed. This means that in Scala a type 'String' is only used as a string throughout a program. Only a type 'String' can be held in the variable’s memory location.

 

Type Inference

Scala has built in type inference so that programmers often do not have to infer the type of the variables. Before demonstrating type inference, we need to first know the definition of polymorphic methods, and generic classes.

 

Polymorphic Methods

Methods can be parameterized by type, and value. In the code example below, 
we define the function 'foo' to take in a parameter of type 'A' and a value 'x' of type A and value y of type 'Int'. Polymorphic methods specify type variables. This can make code more generic, but keeping the advantages of static typing.

 

Generic Classes

Generic classes are like polymorphic methods. They take in type parameters in square brackets.

When using generic classes and polymorphic methods in scala, scala uses type inference. For example, in the code below the declaration of 'first_value' and 'second_value' is identical. Scala infers from the parameter '5' that its type is 'Int'.

 

Other Interesting features?

Interoperability with Java

A useful feature of scala is its interoperability with Java source code. This means that Java libraries can be called, and used from a scala program. This seamless integration is a result of the fact that Scala syntax is a superset of Java sytnax. The sample code below shows a program that builds a GUI based off of imported Java libraries.

 

Lazy Evaluation

Another interesting feature in Scala is that it uses lazy evaluation. This means that values are only evaluated when they are needed. This is demoed in the code below. Lazy evaluation can become useful in building an application when a variable is declared, but its value is dependant on another value not available yet. The programmer then needs to make sure that when the lazy val is used, its dependancy is available.

 

Mutable and Immutable Data

Scala lets programmers define mutable data with the declaration 'var', and immutable data with 'val'. Declaring a variable with 'val' will not allow the variable to be reassigned. Any attempt at redeclaring immutable data will result in a 'reassignment to val error'. It is important to note, that although immutable data cannot be reassigned, their values can be changed. The value just cannot be made to point to a different value at a different memory location. The code below shows an example of this. The list 'aliens' consists of instances of a class 'Alien' which has a mutable attribute name. The name of the Aliens inside the mutable list can be changed, but we cannot add or remove elements from the list because this would require us to reassign it to a new variable. Having support for mutable types helps keep functions in a Scala program pure, with no side effects.

 

How does Scala compare with other languages?

Like Haskell, Scala can be used as a purely functional language. However unlike Haskell it is also purely object oriented like Java. Scala shares similar features with many languages because of both of its functional and object-oriented paradigm, but is most similar to Java because it runs in the Java Virtual Machine environment.

According to Google’s benchmark report, the advantage that Scala holds over Java, is its conciseness, and code complexity optimizations due do it incorporating both functional and object-oriented features into one language. However, Scala does have its disadvantages in that it is a complex language and thus has a high learning curve for new programmers. Also, many developers argue that conciseness often comes at the cost of readability. An example is the function declaration below. One can argue, that although the code is concise, it lacks expressiveness.

 

This article was written by Grazietta Hof and posted originally on Medium